tags:

views:

18

answers:

1

I would like to filter certain fields in my database which are Null, 0, or ''. Unfortunately, using NULL in an IN condition fails to return anything...I believe this is due to NULL comparisons in SQL evaluating as UNKNOWN. For example:

$filterField = $this->Model->find('list', array(
     'fields' => array('id','name'),
     'recursive' => 0,
     'conditions' => array('Model.related_string' => array(Null, 0, '')),
     'order' => array('Model.name ASC') 
     )
  );

This always returns no errors and zero rows because the resulting query has SELECT ... WHERE 'Model'.'related_string' IN (NULL, 0, ''). However, if I want to OR the NULL condition separately, I can't seem to do it with PHP's array syntax. I will overwrite the values. For example:

          $conditions['OR'] = array(
             'Model.related_string' => array('', 0),
             'Model.related_string' => NULL);

Failure. This will only search for NULL entries when the value for the 'Model.related_string' key is overwritten. Am I stuck writing two finds?

+1  A: 

Just wrap it in one more array:

$conditions['OR'] = array(
    array('Model.related_string' => array('', 0)),
    array('Model.related_string' => NULL)
);

I'd suggest that if you have that many different values to test against, what you should first and foremost think about is to standardize them to one possible value.

deceze
This works. Your suggestion is good, and we do need to have standardized default values in the case of string and integer types. Thank you for the prompt response.
Michael