SQL LIKE Condition

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.

Try this:




'criteria'=>array(

        'condition'=>'subcats LIKE :subcats',

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

),



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?

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'].'|%'),

),