CDbCriteria Search

Yii framework model have search() function. We can create more function like search. I created new function ‘searchcategory’ and i added some custom function to searchcategory() function.

Example

 public function searchcategory()
 {
        $criteria=new CDbCriteria;
        $condition='';
        $categoryname=$this->categoryname;
        
        if($this->categoryname1!=''){
            $categorystring=$this->likecategoryname($this->categoryname1);
            if($categorystring!=''){                       
                $condition="categoryidIN (".$categorystring.")";               
            }
        }
        if($categoryname!=''){
            $categoryidstring=$this->likecategoryname($categoryname);
            if($categoryidstring!=''){                       
                if($condition!='')
                    $condition.=' AND ';
                $condition.="categoryid IN (".$categoryidstring.")";               
            }            
        }

        if($condition!=''){
            $criteria->condition=$condition;
        }                  
        return new CActiveDataProvider($this, array(
                         'criteria'=>$criteria,
         ));
 }

    public function likecategorynane($categoryname1){
        $categorymodel=Category::model()->
                               findAll(array('select'=>'categoryid',
                                           'condition'=>'categoryname like 
                                                      "'.$categoryname1.'"'));

        $categoryidarray=CHtml::listData($categorymodel,'categoryid','categoryid');
        $categoryid_string=implode("','",$categoryidarray);
        return $categoryid_string;
    }