CakePHP 3.8
I am trying to find the first article in each selected category in a cell
In Categories table :
$this->hasOne(‘FirstArticle’, [
‘className’ => ‘Articles’,
‘foreignKey’ => ‘category_id’,
‘strategy’ => ‘select’,
‘sort’ => [‘FirstArticle.sort’ => ‘ASC’]
]);
in the display method of a cell :
$this->loadModel(‘Categories’);
$query = $this->Categories->find()
->select([‘Categories.id’, ‘Categories.name’])
->where([‘Categories.id >’ => 1, ‘Categories.sidebar’ => 1])
->contain([
‘FirstArticle’ => [‘fields’ => [‘FirstArticle.id’, ‘FirstArticle.category_id’, ‘FirstArticle.name’, ‘FirstArticle.slug’, ‘FirstArticle.sort’]]
])
->order([‘Categories.lft’ => ‘ASC’])
->limit(6)
;
sort = name of a column in Articles table. In some categories, articles are ordered through this sort column.
This method returns an article ordered by categories order : I mean :
first article of first category , second article of second category etc.
This is not what I want.
Any idea ?