CGridView and search function

I have a cats table and a cats controller (from command line).

This table has a parent_id, so I want at search function to search at cat_code of parent code and not at parent_id of this cat.

How can I do that?


$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'cats-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(		

		'cat_code',

		'getparent.cat_code',

              array(            // display 'create_time' using an expression

            'name'=>'cat_type',

             'value'=> 'mytype($data->cat_type)',

         ),

		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); 


public function search() {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;


               $criteria->compare('cat_code',$this->cat_code,true);


        $criteria->compare('parent_id',$this->parent_id);

.....

I don’t really understand how your tables are linked… I got lost… is this a recursive table? :blink:

But my guess is that your parent_id links to a cat table by way of id but you want to display the code? (that was probably clear as mud as well).

Anyways look at instead just changing your view file “_search.php”. This way you get a parent_id to send to your compare statement (which you’ll need for the criteria unless your gonna do some table joining… avoid joining if possible). Unless you’re doing something more complicated… in that case post a little more of the database structure.

The table schema is cat_id,cat_code ,parent_id , cat_type.

parent_id look at another cats record (cat_id), the form created from yii command line so I let the name parent_id at this field but now it looks for cat_code of ‘parent’ record and not at parent_id of child I hope this is clear (maybe the right name is parent_cat_code or something).

So with the form element ‘parent_id’ I want to find the cat_id of parent record with the cat_code filled there and compare it with the parent_id of records

[edit]I manage to solve it.

I change the code to this but does not work properly, when I set a cat_code of a parent cat the searchg return no cats but the right result is 1 cat.what is wrong?


// Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;

        $criteria->condition.=" 1=1";


        if (!empty ($this->cat_code)) {

            $criteria->condition.="  and cat_code=:cat_code";

            $criteria->params+=array(':cat_code'=>$this->cat_code);

        }

        if (!empty ($this->cat_type)) {

            $criteria->condition.=' and  cat_type=:cat_type';


            $criteria->params+=array(':cat_type'=>$this->cat_type);

        }

        if (!empty ($this->parent_code))

//print_r//("select cat_id from cats where cat_code like '%".$this->parent_id."%'");

        {

            $criteria->condition.=" and parent_id in (select cat_id from cats where cat_code =:parent_id)";

            $criteria->params+=array(':parent_id'=>"%".$this->parent_code."%");

        }

        return new CActiveDataProvider('cats', array(

                        'criteria'=>$criteria,

        ));

I add the parent_code to the form instead of parent_id