解释cakephp如何处理和输出结果数组。
为了获得相关结果,您必须在每个模型中定义它,如下所示。
代码集项目模型
<?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;
}
}
?>
上面将输出索引为 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 方法中使用 find('all') 时,它会如下所示。
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
)
)
)
)