Hi net16,
First, introduce a virtual attribute for the search parameter, and declare it to be ‘safe’ on ‘search’ scenario.
class MyModel extends CActiveRecord {
public $color_search;
...
public function rules() {
return array(
...
array('xxx, yyy, color_search', 'safe', 'on'=>'search'),
);
}
...
public function attributeLabels()
{
return array(
'xxx' => 'XXX',
'yyy' => 'YYY',
'color_search' => 'Color',
...
);
}
...
}
This ‘color_search’ is the holder of the color name that will be searched.
Because you will use it in the search form, give it a label, too.
And then in your searchnew method, compare ‘color_search’ with the colors:
$criteria = new CDbCriteria;
$criteria->compare('second', $this->color_search, true);
$criteria->compare('third', $this->color_search, true, 'OR');
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
The point here is the 4th parameter of compare().
And if you have to compare other attributes than the colors at the same time, then:
$criteria = new CDbCriteria;
$criteria->compare('xxx', $this->xxx, true);
$criteria->compare('yyy', $this->yyy, true);
if ($this->color_search != '') {
$criteria2 = new CDbCriteria;
$criteria2->compare('second', $this->color_search, true);
$criteria2->compare('third', $this->color_search, true, 'OR');
$criteria->mergeWith($criteria2);
}
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
In this case you have to use mergeWith().
Look up the class reference of CDbCriteria for compare() and mergeWith().
http://www.yiiframework.com/doc/api/1.1/CDbCriteria