CakePHP: Find where the field is nonzero

I need to select all rows where User.site_url is not null. It is simple enough to do this in a regular MySQL query, but how is this done in CakePHP?

The manual mentions the following:

array ("not" => array ( "Post.title" => null ) ) 

I tried the following, but it still returns everything

 $this->User->find('all', array('conditions' => array('not' => array('User.site_url')))); 
+45
php cakephp
Jul 28 '09 at 21:54
source share
9 answers

I think this is what you mean:

 $this->User->find('all', array( 'conditions' => array('not' => array('User.site_url' => null)) )); 
+95
Jul 28 '09 at 22:00
source share

You just have zero

 $this->User->find('all', array('conditions' => array('not' => array('User.site_url'=>null)))); 
+16
Jul 28 '09 at 22:03
source share

In Cake, the WHERE clause is constructed from the "conditions" element by combining keys and values. This means that you can skip providing keys if you want. For example:.

 array('conditions' => array('User.id'=>1)) 

completely equivalent

 array('conditions' => array('User.id = 1')) 

Essentially, you can only solve your problem with this:

 $this->User->find('all', array('conditions' => array('User.site_url IS NOT NULL'))); 
+12
Sep 28 '11 at 15:04
source share

You can also try this,

 $this->User->find('all', array('conditions' => array('User.site_url <>' => null)); 

This works great for me.

+3
Jun 23 '13 at 5:56 on
source share

This work great for me:

 $this->User->find('all', array('conditions' => array('User.site_url !=' => null)); 
+1
Jan 29 '13 at 3:37 on
source share

Try '' instead of null :

 $this->User->find('all', array('conditions' => array('User.site_url <>' => '')); 
+1
Sep 19 '15 at 7:33
source share

For a simple query:

 $this->User->find('all', array( 'conditions' => array( 'User.site_url IS NOT NULL' )); 

For cakephp 3.X

  $table = TableRegistry::get('Users'); $assessmentComments = $table ->find() ->where(function (QueryExpression $exp, Query $q) { return $exp->isNotNull('site_url'); }) ->all(); 
+1
Jan 05 '16 at 13:10
source share

this area is correct! (Ctlockey)

 $this->User->find('all', array('conditions' => array('not' => array('User.site_url' =>null)))); 

However, I have used conflicting results with different versions of MySql and MariaDb. I find that a little straightforward SQL is not so bad as to ensure return integrity.

So I did the following:

 $Obj->find()->where(['field_a IS NULL', 'field_b IS NOT NULL'])->all(); 
+1
Jul 25 '18 at 21:57
source share

Works for me

 $this->set('inventory_masters',$this->InventoryMaster->find('all',array('order'=>$orderfinal,'conditions' => array('InventoryMaster.id' => $checkboxid,'not' => array('InventoryMaster.error'=>null))))); 
0
Jun 27 '16 at 10:53 on
source share



All Articles