我刚刚编写了我的第一个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'
)
)
Run Code Online (Sandbox Code Playgroud)
对我来说,似乎没有办法在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'
));
Run Code Online (Sandbox Code Playgroud)
看起来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();
Run Code Online (Sandbox Code Playgroud)
您可以测试 where 子句是否正确。
var_dump($query->getWhere());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
674 次 |
| 最近记录: |