在CakePHP3.4中,如何使用fluent查询生成器在匹配内部创建子查询?
下面的代码创建了将在CakePHP3.4中,如何使用fluent查询生成器在匹配内部创建子查询?,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,下面的代码创建了将任务与最新决策通过任务决策联接表的查询。如果不使用普通查询,但使用CriteriaBuilderinteraface /** Root of this query object is Task * Decision is direct child */ $query->matching("Decisions", function (Query $q) use ($decision) { $q->where(["decision IN" =>
任务
与最新决策
通过任务决策
联接表的查询。如果不使用普通查询,但使用CriteriaBuilder
interaface
/** Root of this query object is Task
* Decision is direct child
*/
$query->matching("Decisions", function (Query $q) use ($decision) {
$q->where(["decision IN" => $decision]);
$q->where([
"Decisions.id" => $q->newExpr("SELECT MAX(td.decision_id) FROM tasks_decisions td WHERE td.task_id=Tasks.id")
]);
return $q;
});
而不是这个
$q->where(["Decisions.id" => $q->newExpr("SELECT MAX(td.decision_id) FROM tasks_decisions td WHERE td.task_id=Tasks.id")]);
试一试
这只是一个例子,只是根据需要建立条件
请参阅此处的手册
$q->where(["Decisions.id" => $this->TaskDecision->find()->all();