Yii Framework Forum: Cdbcriteria Compare With Value Of 0. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cdbcriteria Compare With Value Of 0. Comparing data when the data sent up from the client is 0. Rate Topic: -----

#1 User is offline   dillongilmore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-February 13
  • Location:Henderson, NV

Posted 28 March 2013 - 05:59 PM

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

#2 User is offline   dillongilmore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-February 13
  • Location:Henderson, NV

Posted 01 April 2013 - 03:56 PM

bump
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 02 April 2013 - 03:22 AM

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

#4 User is offline   dillongilmore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-February 13
  • Location:Henderson, NV

Posted 04 April 2013 - 03:09 PM

View PostKeith, on 02 April 2013 - 03:22 AM, said:

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

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 04 April 2013 - 04:18 PM

Have you tried it? It should work.
1

#6 User is offline   dillongilmore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-February 13
  • Location:Henderson, NV

Posted 05 April 2013 - 03:15 PM

View PostKeith, on 04 April 2013 - 04:18 PM, said:

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

#7 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 07 April 2013 - 05:15 AM

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

#8 User is offline   glyph 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 20-April 12
  • Location:Michigan, USA

Posted 10 May 2013 - 10:15 AM

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

-------------------------------------------------
Glyph Imaging and Data Services
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users