cakephp 3.x从父类别

时间:2015-07-13 09:44:23

标签: cakephp greatest-n-per-group cakephp-3.0

Postcategories(树形结构)有很多帖子。现在父postcategories将有Postcategories.parent_id null。

$postcategories = TableRegistry::get('Postcategories',[
            'className' => 'App\Model\Table\PostcategoriesTable', 
            'table' => 'postcategories'
    ]);    
//I get $parents post categories whose parent_id is null and the bellow code is for finding latest 4 posts from each child for typical parent.

$childs = $postcategories->find('children',['for' => $parent['id'],
            'fields'=>['Postcategories.id','Postcategories.name']])->contain([
            'Posts'=>['strategy'=>'subquery','queryBuilder' =>function ($q) use ($parent){
                        return $q->select(['Posts.id', 'Posts.title','Posts.postcategory_id'])
                        ->order(['Posts.created'=> 'DESC'])                         
                        ->limit(4);}]])->hydrate(false)->toArray();

这段代码从数据库中获取总共4个帖子,而我正在尝试获取父类别的每个子类别的4个帖子。怎么办我可以发布更多代码以便更清晰。

0 个答案:

没有答案