我有关系Postcategory hasMany Posts and Post hasOne User。这是我的代码,它提取帖子,但我无法获得每个帖子的用户数据。
$parents = $postcategories->find('all',[
'fields'=>['Postcategories.id','Postcategories.name'],
])->contain(['Posts'=>function ($q) {
return $q->select(['id', 'title','postcategory_id'])
->where(['Posts.active' => 1])
->order('Posts.created DESC')
->limit(5)
;}])
->where(['Postcategories.parent_id is null','Postcategories.active'=>1])
->hydrate(false)->toArray();
此外,我只想为每个类别添加5个最新帖子。此代码仅为第一个类别提取5个帖子,为剩余类别提取空白数组。