我正在尝试在CakePHP 3.3.15应用程序中实现全局搜索机制- 这涉及在父表中搜索值以及在所有关联表中搜索相同的值。我正在使用matching(),但我找不到一种使用 OR 将多个matching() 组合到一个查询对象中的方法。
看起来matching()会缩小结果集范围,并且连续调用matching()将使用先前matching()调用修改的对象。
这就是我所拥有的:
$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
Run Code Online (Sandbox Code Playgroud)
到目前为止我还尝试过什么:
innerJoinWith()而不是matching() - 结果相同,只是结果集缺少关联字段(正如CookBook中所述)$this->$modelName->find()->contain('ChildTable', function ($q) { return …