Cdbcriteria Compare With Value Of 0.

Hello, I am trying to use CDbCriteria()->compare attribute to filter out data. Here is an example of a dropdown, one of my filters use with the name ‘numbers’.

0 => "Zero",

1 => "One",

2 => "Two"

So when the user does One or Two, the client sends 1 or 2 respectively to the server where I do something like this.




$criteria = new CDbCriteria();

$criteria->compare('numbers', $this->numbers);



With One or Two, the Grid gets updated and everything works. With Zero nothing is returned, I assume it has something to do with the fact that 0 is also null, so nothing is returned. I need these indexes to stay as is and get 0 to compare correctly.

I would be happy to give any other information you may need to help me. Thank you.

bump

I don’t know if it’s the best way, but I replace the call to compare with something like the following:




    if ($this->numbers !== null && $this->numbers !== '')

        $criteria->addColumnCondition(array('numbers'=>$this->numbers));



It allows you to only explicitly specify when the comparison should be performed.

This doesn’t really help me, I need to be able to filter by 0. This will just ignore 0, which is not what I want.

Have you tried it? It should work.

Okay, I tried it and I am getting the same results as before. I am error logging




error_log($this->number);



I see that it is sending up 0, so the conditional is passing, but I am still getting nothing outputted on the grid.

I think it may be because the database is storing null, instead of 0, but I will need to search for both.

Also, after looking through the tables, some of the records aren’t even found, which could pose as another problem. I tried doing this so far:




if ($this->number == 0) {

    criteria->addColumnCondition(array('number' => new CDbExpression('NULL')));

}



But that is not working.

I’m not quite sure about your set up, but you shouldn’t use the CDbExpression in that condition. Try




if ($this->number == 0) {

    $criteria->addColumnCondition(array('number' => null));

}



Also, you seem to be switching between number and numbers in your examples. Obviously, make sure you use the right one in the code.

Also be sure any conditionals wrapping your criteria compare statement are using strict equality:




$criteria->with['samples'] = array('select'=>'samples.id','together'=>true);

if($this->tumor_status_search !== '') {

	$criteria->compare( 'samples.tumor', $this->tumor_status_search, false);            

}