2013-02-24 96 views
0

在cakephp中我有代码集和codesetitems。 codesetitems属于代码集,所以在我的codesetitems我有belongsTo ='Codeset'。但在我看来,我似乎无法调用$ codeset ['Codesetitem'] ['id']。它说未定义的索引Codesetitem。我已经检查过蛋糕文件。一个代码集可以有许多代码集。cakephp模型关联(查看)

+0

您的CodesetController是否传递了一个正确构造的$ codeset数组,其实际上包含Codesetitem对象?您可以在控制器中$ this-> log($ codeset)然后在PHP错误文件中查找输出。如果您使用的是传统网页,我认为一个debug()语句将以HTML格式输出输出。 – 2013-02-24 23:57:47

+0

你最好a)检索数据,并b)发送到视图? – Dave 2013-02-25 02:28:47

回答

0

关于cakephp如何处理和输出结果数组的说明。

为了获得相关的结果,您必须在每个模型中定义它,如下所示。

CodesetItem模型

<?php 
class CodesetItem extends AppModel 
{ 
    var $name = 'CodesetItem'; 

    var $belongsTo = array 
    (
     'Codeset' => array 
     (
      'className' => 'Codeset', 
      'foreignKey' => 'codeset_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 
} 
?>

代码集模式

<?php 
class Codeset extends AppModel 
{ 
    var $name = 'Codeset'; 

    var $hasMany = array 
    (
     'CodesetItem' => array 
     (
      'className' => 'CodesetItem', 
      'foreignKey' => 'codeset_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 
} 
?>

代码集控制器

<?php 
class CodesetsController extends AppController 
{ 
    var $name = 'Codesets'; 

    function beforeFilter() 
    { 
     parent::beforeFilter(); 
    } 

    function index() 
    { 
     $codesets = $this->Codeset->find('first'); 
     pr($codesets); 
     exit; 
    } 
} 
?>

ABOV Ë会出来把代码组阵列的0意味着指数如下

Array 
(
    [Codeset] => Array 
    (
     [id] => 121 
     [name] => Gwoo the Kungwoo 
     [created] => 2007-05-01 10:31:01 
    ) 
    [CodesetItem] => Array 
    (
     [0] => Array 
      (
       [id] => 123 
       [codeset_id] => 121 
       [title] => On Gwoo the Kungwoo 
       [body] => The Kungwooness is not so Gwooish 
       [created] => 2006-05-01 10:31:01 
      ) 
     [1] => Array 
      (
       [id] => 124 
       [codeset_id] => 123 
       [title] => More on Gwoo 
       [body] => But what of the ‘Nut? 
       [created] => 2006-05-01 10:41:01 
      ) 
    ) 
)

但是当你找到方法使用查找(“全部”),它下面会出。

Array 
(
    [0] => Array 
    (
     [Codeset] => Array 
     (
      [id] => 121 
      [name] => Gwoo the Kungwoo 
      [created] => 2007-05-01 10:31:01 
     ) 
     [CodesetItem] => Array 
     (
      [0] => Array 
       (
        [id] => 123 
        [codeset_id] => 121 
        [title] => On Gwoo the Kungwoo 
        [body] => The Kungwooness is not so Gwooish 
        [created] => 2006-05-01 10:31:01 
       ) 
      [1] => Array 
       (
        [id] => 124 
        [codeset_id] => 121 
        [title] => More on Gwoo 
        [body] => But what of the ‘Nut? 
        [created] => 2006-05-01 10:41:01 
       ) 
     ) 
    ) 
    [1] => Array 
    (
     [Codeset] => Array 
     (
      [id] => 121 
      [name] => Gwoo the Kungwoo 
      [created] => 2007-05-01 10:31:01 
     ) 
     [CodesetItem] => Array 
     (
      [0] => Array 
       (
        [id] => 123 
        [codeset_id] => 121 
        [title] => On Gwoo the Kungwoo 
        [body] => The Kungwooness is not so Gwooish 
        [created] => 2006-05-01 10:31:01 
       ) 
      [1] => Array 
       (
        [id] => 124 
        [codeset_id] => 121 
        [title] => More on Gwoo 
        [body] => But what of the ‘Nut? 
        [created] => 2006-05-01 10:41:01 
       ) 
     ) 
    ) 
)