【问题标题】:Kohana 3 ORM missing NOT_WHERE?Kohana 3 ORM 缺少 NOT_WHERE?
【发布时间】:2012-03-22 21:55:08
【问题描述】:

在 Kohana 中,我是否可以轻松地否定嵌套布尔集?

SELECT * FROM table WHERE NOT ( (id = 1 OR name like 'bob') and col2 = 333 )

我得到了这个代码...

$db->and_where(col2, 333)
   ->and_where_open()
        ->or_where(id, 1)
        ->or_where(name, bob)
   ->and_where_close

生成

SELECT * FROM table WHERE (id = 1 OR name like 'bob') and col2 = 333 

我如何将所有这些东西包装在 NOT 中?

【问题讨论】:

    标签: php kohana kohana-3 kohana-orm


    【解决方案1】:

    请试试这个,这不是你想要的,但会给你一个意见:

    $db->and_where(col2, 333)
       ->and_where_open()
        ->or_where('id', '!=', 1)
        ->or_where('name', '!=', 'bob')
        ->and_where_close
    

    在手册中我看到这句话:“限制查询结果是使用 where()、and_where() 和 or_where() 方法完成的。这些方法采用三个参数:列、运算符和值。”

    这里是链接:http://kohanaframework.org/3.2/guide/database/query/builder

    【讨论】:

    • 逻辑相反的作品,但我试图通过读取存储逻辑规则的外部数据结构来创建查询,所以这种动态转换让我不舒服......嗯 A 表示努力。
    • 实际上,你所拥有的并不是对立的。你有 (id != 1 OR name != 'bob') 和 col2 = 333 不等于 NOT((id = 1 OR name like 'bob') and col2 = 333)
    【解决方案2】:

    DB::Expr 来救援!

    你必须这样写:

    $query = DB::select('users')
        ->where(DB::Expr("NOT ( (id = 1 OR name like 'bob') and col2 = 333 )"))
        ->execute();
    

    如果我没记错语法。可能 DB::Expr 必须打包在一个数组中,我已经很久没有和 Kohana 合作了。

    API 参考:

    http://kohanaframework.org/3.2/guide/api/Database_Expression

    ORM 对基本操作很有用,但对于有些复杂的查询,它们也可以成为真正的 PITA。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多