php CakePHP:查找字段不为空的地方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP: Find where field is not null
提问by DanCake
I need to select all rows where User.site_url is not null. It's simple enough to do this in a regular MySQL query but how is this done in CakePHP?
我需要选择 User.site_url 不为空的所有行。在常规 MySQL 查询中执行此操作很简单,但在 CakePHP 中如何执行此操作?
The manual mentions the following:
手册提到了以下内容:
array ("not" => array (
"Post.title" => null
)
)
I have tried the following but it's still returning everything
我已经尝试了以下但它仍然返回一切
$this->User->find('all', array('conditions' => array('not' => array('User.site_url'))));
回答by g33kz0r
I think this is what you mean:
我想这就是你的意思:
$this->User->find('all', array(
'conditions' => array('not' => array('User.site_url' => null))
));
回答by PetersenDidIt
Your just missing the null
你只是错过了空值
$this->User->find('all', array('conditions' => array('not' => array('User.site_url'=>null))));
回答by GwynBleidd
In Cake, a WHERE condition is constructed from 'conditions' element by joining keys and values. That means that you can actually skip providing the keys if you like. E.g.:
在 Cake 中,WHERE 条件是通过连接键和值从 'conditions' 元素构造的。这意味着如果您愿意,您实际上可以跳过提供密钥。例如:
array('conditions' => array('User.id'=>1))
is completely equivalent to
完全等同于
array('conditions' => array('User.id = 1'))
Essentially, you can solve your problem by just this:
基本上,您可以通过以下方式解决您的问题:
$this->User->find('all', array('conditions' => array('User.site_url IS NOT NULL')));
回答by Souvick Dey
You can also try this,
你也可以试试这个
$this->User->find('all', array('conditions' => array('User.site_url <>' => null));
This works fine for me..
这对我来说很好用..
回答by Fury
For simple query:
对于简单查询:
$this->User->find('all', array(
'conditions' => array(
'User.site_url IS NOT NULL'
));
For cakephp 3.X
对于cakephp 3.X
$table = TableRegistry::get('Users');
$assessmentComments = $table
->find()
->where(function (QueryExpression $exp, Query $q) {
return $exp->isNotNull('site_url');
})
->all();
回答by aminulsujon
Please try ''rather than null:
请尝试''而不是null:
$this->User->find('all', array('conditions' => array('User.site_url <>' => ''));
回答by Chanraksmey
This work fine for me:
这对我来说很好:
$this->User->find('all', array('conditions' => array('User.site_url !=' => null));
回答by Erismar B. Vieira
this scope is correct!(ctlockey)
这个范围是正确的!(ctlock)
$this->User->find('all', array('conditions' => array('not' => array('User.site_url' =>null))));
However I using with different versions of MySql and MariaDb returned inconstant results. I believe that a little bit of direct sql is not that bad so to ensure the integrity of the return.
但是,我使用不同版本的 MySql 和 MariaDb 返回了不稳定的结果。我相信一点点直接的sql也没有那么糟糕,以确保返回的完整性。
Therefore, I did the following:
因此,我做了以下事情:
$Obj->find()->where(['field_a IS NULL', 'field_b IS NOT NULL'])->all();
回答by princespn
Its working for me
它对我有用
$this->set('inventory_masters',$this->InventoryMaster->find('all',array('order'=>$orderfinal,'conditions' => array('InventoryMaster.id' => $checkboxid,'not' => array('InventoryMaster.error'=>null)))));

