我阅读了食谱,但我不知道如何在单个查询中组合 amatching()
和 a orWhere()
。
示例:我有Photo
那个属于Album
. 两者都有active
领域。所以我正在尝试编写一个findInactive()
方法。“非活动”照片的active
字段为false
或active
与字段为的相册相匹配false
。
像这样的东西:
public function findInactive(Query $query, array $options)
{
$query->matching('Albums', function ($q) {
return $q->where(['Albums.active' => false]);
})
->orWhere(['Photos.active' => false])
->enableAutoFields(true);
return $query;
}
但这不起作用:
'SELECT [...] FROM photos Photos INNER JOIN photos_albums Albums ON (Albums.active = :c0 AND Albums.id = (Photos.album_id)) WHERE Photos.active = :c1'
怎么做?谢谢。
编辑
也许一个可能的解决方案是使用contain()
:
$query->contain(['Albums => ['fields' => ['active']]])
->where(['Photos.active' => false])
->orWhere(['Albums.active' => false]);
但是不能使用matching()
orinnerJoinWith()
吗?