Getting the latest article of each category

Having Articles belong to Categories, how can I find the latest article of each category ?

After reading this page : https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html, I tried to do the same :
in Categories table :
$this->hasOne(‘FirstArticle’, [
‘className’ => ‘Articles’,
‘foreignKey’ => ‘category_id’
]);

and :
$query = $this->Categories->find()
->select([‘Categories.id’, ‘Categories.parent_id’, ‘Categories.name’])
->where([‘Categories.id in’ => $categories_ids])
->contain([
‘FirstArticle’ => [
‘fields’ => [‘FirstArticle.id’, ‘FirstArticle.category_id’, ‘FirstArticle.name’],
‘strategy’ => ‘select’,
‘queryBuilder’ => function ($q) {
return $q->order([‘FirstArticle.created’ =>‘DESC’])->limit(1);
}
]
]);
but this does not work. You only get one single article.

Hi,

in Categories table :
$this->hasOne(‘FirstArticle’, [
‘className’ => ‘Articles’,
‘foreignKey’ => ‘category_id’,
‘strategy’ => ‘select’,
‘sort’ => [‘FirstArticle.created’ => ‘DESC’]
]);

then in your controller
$query = $this->Categories->find()
->select([‘Categories.id’, ‘Categories.parent_id’, ‘Categories.name’])
->where([‘Categories.id in’ => $categories_ids])
->contain([‘FirstArticle’ => [
‘fields’ => [‘FirstArticle.id’, ‘FirstArticle.category_id’, ‘FirstArticle.name’]
]]);

Thank you.
I forgot you can have multiple associations in the same table.