[英]Lithium - Find tree based on relationships
I have created a RESTful PHP web service using Lithium which contains comments, each comment can have a parent comment allowing comments to be infinitely recursive. 我使用Lithium创建了一个RESTful PHP Web服务,该服务包含注释,每个注释可以有一个父注释,从而允许注释无限递归。
I have set up the relationship within my model using the correct key. 我已经使用正确的密钥在模型中建立了关系。
My data is currently formatted list this (using Model::()
): 我的数据当前已格式化(使用Model::()
):
Array
(
[1C19FA9D-0432-A382-5236-2C59E0967F58] => Array
(
[id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] =>
[user_id] => 4
[comment] => This is a test
[created] => 2012-03-16 16:41:33
[updated] =>
)
[3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0] => Array
(
[id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[user_id] => 543
[comment] => Testing
[created] => 2012-03-16 17:25:47
[updated] =>
)
[4CFD2D8B-D05F-7C8A-E2A9-38D5677280A9] => Array
(
[id] => 4CFD2D8B-D05F-7C8A-E2A9-38D5677280A9
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[user_id] => 53
[comment] => A Test
[created] => 2012-03-16 17:25:38
[updated] =>
)
)
And I would prefer it was formatted like this 我希望它是这样格式化的
Array
(
[0] => Array
(
[id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] =>
[user_id] => 4
[comment] => This is a test
[created] => 2012-03-16 16:41:33
[updated] =>
[comment] => Array(
[0] => Array
(
[id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[user_id] => 543
[comment] => Testing
[created] => 2012-03-16 17:25:47
[updated] =>
)
[1] => Array
(
[id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
[page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
[parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
[user_id] => 543
[comment] => A Test
[created] => 2012-03-16 17:25:47
[updated] =>
)
)
)
)
Is there a recursing function built in to Lithium or is this something I would have to create myself? Lithium是否内置了递归函数,或者我必须自己创建此函数? Please also note the changes in Keys. 另请注意密钥中的更改。
To fix this I actually ended up generating my own array from the results as follows: 为了解决这个问题,我实际上最终从结果生成了自己的数组,如下所示:
public function index($page_id) {
$page = Pages::first($page_id);
$site = Sites::first($page->site_id);
$comments = Comments::all(array('conditions' => array('page_id' => $page->id, 'comment_id' => NULL)));
if ($this->request->type == 'JSON')
$comments = $this->_recursiveComments($comments, $page);
return compact('comments', 'page', 'site');
}
protected function _recursiveComments($comments, $page) {
foreach($comments as $c) {
$children = Comments::all(array('conditions' => array('comment_id' => $c->id)));
$c->children = $children;
$this->_recursiveComments($children, $page);
}
return $comments;
}
That way I could just do a foreach($comments as $key => $value)
loop and avoid the random keys. 这样我就可以做一个foreach($comments as $key => $value)
循环并避免使用随机键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.