cakephp - CakePHP3.x fetching associated data from controller -
i have relations postcategory hasmany posts , post hasone user. here code fetches posts not able users data each post.
$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 null','postcategories.active'=>1]) ->hydrate(false)->toarray();
also want 5 latest posts each category. code fetches 5 posts first category , blank array remaining categories.
Comments
Post a Comment