I'm trying to implement a global search mechenism in my CakePHP 3.3.15 app - this involves searching the Parent table for a value plus searching all the associated tables for the same value. I'm using matching()
, but I can't find a way to combine multiple matching() into one query object using OR.
It looks like matching() will narrow down the result set and calling matching() consecutively will use the object modified by the previous matching() call.
This is what I have:
$query = $this->$modelName->find();
$query->matching('Colonies', function ($q) {
return $q->where(['OR' => [
'Colonies.colony_id' => 10003,
'Colonies.project_id' => 6
]]);
});
debug($query->count()); //returns 4 entries
$query->matching('Projects', function ($q) {
return $q->where(['OR' => [
'Projects.id' => 1,
'Projects.project_status_id' => 3
]]);
});
debug($query->count()); //returns 1 entry. However, when the previous matching() is removed, it will return 2 entries
What else I have tried so far:
- using
innerJoinWith()
instead of matching() - same result, just the result set is lacking associated fields (exactly as said in the CookBook) - using
$this->$modelName->find()->contain('ChildTable', function ($q) { return $q->where(['ChildTable.FieldName' => 'some value']});
- this does not seem to work at all (doesn't filter the result set) - Combining the result sets manually to force OR behavior - I'm using pagination later in the code, so I can't work on the result sets directly - I need a single Query object to pass to the paginator.
Any help greatly appreciated!