I am trying to get products under the Categories. Here Categories hasMany
products. I am trying to fetch 3 products under the category. I have tried below code in controller
public function test()
{
$this->viewBuilder()->layout(false);
$Menus = $this->loadModel( 'Categories' );
$menu_top = $Menus
->find()
->contain(['Subcats','Products'=>function($q{return $q->limit(3);}])
$this->set('menu_top',$menu_top);
}
After @ndm comment I am going to add more details.
Example I have 3 Categories, and all categories has lots of products
A B C
->p1 ->p1 ->p1
->p2 ->p2 ->p2
->p3 ->p3 ->p3
->p4 ->p4 ->p4
... ... ...
I am trying to fetch 3 products per categories like below
A B C
->p1 ->p1 ->p1
->p2 ->p2 ->p2
->p3 ->p3 ->p3
If I used ->contain(['Subcats','Products'=>function($q{return $q->limit(3);}])
It actually limiting my total products. Example now I am getting like
A B C
->p1 -> ->
->p2 -> ->
->p3 -> ->
Here I need row wise limit not total limit on products.