我正在编写我的第一个 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());
关于php - 在 Phalcon\Mvc\Model\Criteria 中组合 and 和 or 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081983/