在cakephp我有代码集和codesetitems。 codesetitems属于代码集所以在我的代码集我有belongsTo ='代码集'。但在我看来,我似乎不能调用$ codeset ['Codesetitem'] ['id']。它说未定义的索引Codesetitem。我已经检出了蛋糕文档。一个代码集可以有多个代码集。
In cakephp I have codesets and codesetitems. codesetitems belong to codesets so in my codesetitems I have belongsTo = 'Codeset'. But in my view I don't seem to be able to call $codeset['Codesetitem']['id']. It says undefined index Codesetitem. I already checked out the cake documentation. One codeset can have many codesetitems.
推荐答案关于 cakephp 并输出结果数组。
为了获得关联结果,必须在每个模型中定义如下。
In order to get associated result you must define it in each model as below.
CodesetItem模型
<?php class CodesetItem extends AppModel { var $name = 'CodesetItem'; var $belongsTo = array ( 'Codeset' => array ( 'className' => 'Codeset', 'foreignKey' => 'codeset_id', 'conditions' => '', 'fields' => '', 'order' => '' ) ); } ?>代码集模型
Codeset Model
<?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' => '' ) ); } ?>代码集控制器
Codeset Controller
<?php class CodesetsController extends AppController { var $name = 'Codesets'; function beforeFilter() { parent::beforeFilter(); } function index() { $codesets = $this->Codeset->find('first'); pr($codesets); exit; } } ?>指数为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 ) ) )但是当你在find方法它将如下所示。
but when you use find('all') in find method it will out as below.
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 ) ) ) )更多推荐
cakephp模型关联(视图)
发布评论