I read the cookbook, but I can not figure out how to combine in a single query a matching()
and a orWhere()
.
Example: I have Photo
that belongs from Album
. Both have the active
field. So I'm trying to write a findInactive()
method. A "inactive" photo has the active
field as false
or matching an album that has the active
fields as false
.
Something like this:
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;
}
But that does not work:
'SELECT [...] FROM photos Photos INNER JOIN photos_albums Albums ON (Albums.active = :c0 AND Albums.id = (Photos.album_id)) WHERE Photos.active = :c1'
How to do? Thanks.
EDIT
Maybe a possible solution is usecontain()
:
$query->contain(['Albums => ['fields' => ['active']]])
->where(['Photos.active' => false])
->orWhere(['Albums.active' => false]);
But is it not possible to use matching()
or innerJoinWith()
?