Yii Framework Forum: SQL LIKE Condition - Yii Framework Forum

Jump to content

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

SQL LIKE Condition Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 04 March 2010 - 07:59 AM

I'm not sure if I'm doin this correctly, but in my database I have a field that stores data in the format: 1|2|3|4

In my CActiveDataProvider method I have the following:

'criteria'=>array(
	'condition'=>'subcats LIKE :subcats',
	'params'=>array(':subcats'=>$_GET['id']),
),


At the time of running this script, $_GET['id'] is 1 but I don't get back any results for the above data list.

The query only needs to match one of the values in the data list. Furthermore it should match the *whole* value, i.e. 1 is OK but 11 or 21 is not OK.
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 04 March 2010 - 08:01 AM

Try this:

'criteria'=>array(
        'condition'=>'subcats LIKE :subcats',
        'params'=>array(':subcats'=>'%'.$_GET['id'].'%'),
),


Don't say what you think, think what you say
The problem is communication! Excess of communication!
1

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 04 March 2010 - 08:10 AM

Cheers, that works but it also matches on values 11 and 21. Any way we could force it to match on the whole values (contained within the | delimiters)?

Maybe create an array from the values and use php in_array function or something like that?
0

#4 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 04 March 2010 - 08:30 AM

I think you may store the valuas alwais this way:
'|val1|val2|val3|'

Note the | at the beginning and at the end!!

And then you can use the criteria like this:
'criteria'=>array(
        'condition'=>'subcats LIKE :subcats',
        'params'=>array(':subcats'=>'%|'.$_GET['id'].'|%'),
),


Don't say what you think, think what you say
The problem is communication! Excess of communication!
1

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