QueryBuilder limit is working for only first row in cakephp 3.2 [duplicate]

Deadly 提交于 2019-12-02 12:28:09

Maybe I'm wrong (so feel free to downvote my answer) but I think it's not feasible using a simple query

in fact cake, when loading associated data, does a single query on the associated table and after that matches the rows with the corresponding ones in the main table.

So you would need to do a mysql query that find the first record of every category. i.e. something like what is described in this article

I think that the only (or maybe the simpler) solution is to loop through your records:

$campaigns= $this->Campaigns->find();

foreach($campaigns as $campaign)
{
    $campaign->campaign_videos = $this->Campaigns->CampaignVideos-find()
        ->where(['campaign_id' => $campaign->id]
        ->order(['id' => 'asc'])
        ->limit(1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!