![](/img/trans.png)
[英]Is there a way to get assembled query by Phalcon\Mvc\Model\Criteria?
[英]Combining and and or operators in Phalcon\Mvc\Model\Criteria
我刚刚编写了我的第一个phalcon应用程序,并且有一个使用Phalcon \\ Mvc \\ Model \\ Criteria过滤查询的问题。
最后我想要这样的查询
SELECT *
FROM table
WHERE
status = 'A' AND
(
title LIKE 'combining%' OR
title LIKE 'phalcon%' OR
(
title LIKE 'criteria%' AND
title LIKE '%phalcon'
)
)
对我来说,似乎没有办法在phalcons模型标准中进行括号。 实现这一目标的唯一方法是编写phql。
我可能会编写类似这样的内容,而不是编写完整的phql,但这样做会有同样的复杂性
<?php
$query = Table::query();
$query->where('status = :status:', array('status' => 'A'));
$query->andWhere('(
title LIKE :title1: OR
title LIKE :title2: OR (
title LIKE :title3: AND
title LIKE :title4:
)
)', array(
'title1' => 'combining%',
'title2' => 'phalcon%',
'title3' => 'criteria%',
'title4' => '%phalcon'
));
看起来Criteria的“where”,“andWhere”和“orWhere”方法将为您添加外括号,然后您可以手动编写内括号。
$query = Model::query()
->where('status = "A"')
->andWhere('title LIKE "combining%" OR
title LIKE "phalcon%" OR
(
title LIKE "criteria%" AND
title LIKE "%phalcon"
)
');
$models = $query->execute();
您可以测试where子句是否正确。
var_dump($query->getWhere());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.