Cakephp查找不带“的线程”;“儿童”;
我正在设置一个API,希望在我的数组中只包含对象,而不是在整个数组中重复添加“Modelname”和“Children”。有办法做到这一点吗?我想象一个循环可以达到目的,但我想不出来Cakephp查找不带“的线程”;“儿童”;,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我正在设置一个API,希望在我的数组中只包含对象,而不是在整个数组中重复添加“Modelname”和“Children”。有办法做到这一点吗?我想象一个循环可以达到目的,但我想不出来 $results = $this->Test->find('threaded', array( 'fields' => array('id', 'parent_id', 'name'), 'order' => array('lft ASC') // or ar
$results = $this->Test->find('threaded', array(
'fields' => array('id', 'parent_id', 'name'),
'order' => array('lft ASC') // or array('id ASC')
));
for ($i = 0; $i <= $this->Test->childCount(1); $i++) {
debug($results[$i]['children']);
}
$this->set(array(
'results' => $results,
'_serialize' => 'results'
));
$results=$this->Test->find('threaded',数组(
'fields'=>数组('id','parent_id','name'),
'order'=>array('lft ASC')//或array('id ASC'))
));
对于($i=0;$i Test->childCount(1);$i++){
调试($results[$i]['children']);
}
$this->set(数组)(
“结果”=>$results,
“\u序列化”=>“结果”
));
似乎您提供的代码对于3.x和2.x来说都不是唯一的,所以我将为它们分享解决方案
3.x
通过将“children”作为finder类型传递到查询中,可以获得树节点的所有子代的平面列表:
$result = $this->Test->find('children', array(
'for' => $record_id, // Notice you have to specify 'for' key!
'fields' => array('id', 'parent_id', 'name'),
'order' => array('lft ASC')
));
如果只想查找节点的直接子节点,请在选项数组中为“direct”键传入true:
$result = $this->Test->find('children', array(
'for' => $record_id, // Notice you have to specify 'for' key!
'fields' => array('id', 'parent_id', 'name'),
'order' => array('lft ASC'),
'direct' => true
));
更多信息:
2.x
要获得2.x中所有子代的平面列表,可以使用TreeBehavior类提供的->children()函数:
$result = $this->Test->children(
$record_id, // ID of record to find children for
false, // Direct = false
['id', 'parent_id', 'name'], // Fields to include in query
'lft ASC' // Order
)
相反,要仅查找直系子代,请将第二个参数作为true传递:
$result = $this->Test->children(
$record_id, // ID of record to find children for
true, // Direct = true
['id', 'parent_id', 'name'], // Fields to include in query
'lft ASC' // Order
)
更多信息:
CakePHP TreeBehavior在处理树数据方面做了大量的工作,消除了很多麻烦。我希望这些信息有帮助 哪个版本的蛋糕?你能举一个例子说明你正在使用的查找查询以及你希望结果的外观吗?更新了我的问题。你能举一个例子说明你希望结果数组的外观吗?你只是想要一个平面阵列,让孩子们和父母在同一深度?