CakePHP 3 - Query builder properly not working for HasMany associations

烈酒焚心 提交于 2019-12-04 20:48:02

Use matching:

$array = [1,2,3];

$this->Workshops->find()
    ->matching('WorkshopServices', function ($q) use($array) {
       return $q->where(['WorkshopServices.service_id IN' => $array])
    });

I updated @GabrielFerreira's query and Grouping the rows by WorkshopServices.workshop_id, This solution meet my Problem

$array = [1,2,3];

$this->Workshops->find()
    ->select([
          'title'=>'Workshops.title',
          'id'=>'Workshops.id',
    ])
    ->matching('WorkshopServices', function ($q) use($array) {
       return $q->where(['WorkshopServices.service_id IN' => $array]);
    })
    ->group(['WorkshopServices.workshop_id']);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!