CakePHP: Find where field is not null

Viewed 82411

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?

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

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

this scope is correct! (ctlockey)

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

Therefore, I did the following:

$Obj->find()->where(['field_a IS NULL', 'field_b IS NOT NULL'])->all();
Related