Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: Find where field is not null

Tags:

php

cakephp

I think this is what you mean:

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

Your just missing the null

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

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.:

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')));

For 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();

You can also try this,

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

This works fine for me..


Please try '' rather than null:

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

This work fine for me:

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