【问题标题】:Lithium Model find query with <> operator使用 <> 运算符的锂模型查找查询
【发布时间】:2014-10-14 08:58:43
【问题描述】:

我正在尝试获取不包括已经在另一个表中的行:

| Content   | ContentSelected
| ––––––––  | ––––––––––––––––––––––––-
| id        | content_id | operation_id
| 1         | 1          | 9999
| 2         | 3          | 1000
| 3

=> 2,3

这是我尝试运行此查询的方式:

Content::find('all', array(
    'joins' => array(new Query(array(
        'source' => 'ContentSelected',
        'constraints' => array('ContentSelected.operation_id' => 9999),
     'conditions' => array(
         'Content.id' => array('<>' => 'ContentSelected.content_id')
));

这里是适配器运行的 SQL 查询:

SELECT * FROM "Content" AS "Content"
JOIN "ContentSelected" ON "ContentSelected"."operation_id" = 1
WHERE ("Content"."id" <> 0);

是否有其他方法可以执行不包括结果的查询,或者强制适配器在 where 子句中写入 ContentSelected.content_id 而不是 0

谢谢

【问题讨论】:

    标签: lithium


    【解决方案1】:

    您可以使用Model::connection()-&gt;read() 在 Li3 中运行本机 sql。看看这个http://www.jblotus.com/2011/10/25/executing-raw-mysql-queries-in-lithium/

    Content::connection()->read($your_sql);
    

    但是在使用这样的解决方案之前。尝试使用模型关系 (http://li3.me/docs/manual/models/relationships.md) 正确实现您的模型,这样您就无需使用本机查询并让模型为您完成工作。

    【讨论】:

    • 这就是我现在正在做的事情,但它以数组的形式返回结果。我必须转换结果才能得到对象。
    • Content 和 ContentSelected(hasOne、hasMany 或 belongsTo?)之间有什么关系,而您需要加入 ?
    • Content hasMany ContentSelected and ContentSelected belongsTo Content
    • 你试过了吗: Content::find('all', array( 'joins' => array(new Query(array( 'source' => 'ContentSelected', 'constraint' => array) ('Content.id' => 'ContentSelected.content_id', 'ContentSelected.content_id' => 数组('' => '9999')))))));
    • 是的,我试过了,但它返回相同的查询和结果
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多