【问题标题】:Combining and and or operators in Phalcon\Mvc\Model\Criteria在 Phalcon\Mvc\Model\Criteria 中组合 and and or 运算符
【发布时间】:2015-06-26 20:53:28
【问题描述】:

我正在编写我的第一个 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'
));

【问题讨论】:

标签: php sql phalcon


【解决方案1】:

看起来 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());

【讨论】:

  • 问题仍然是生成这样的查询变得非常复杂 - 或者可能更复杂......
猜你喜欢
  • 2011-12-28
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2021-12-04
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多