Grid view and filter

Hi,

when I use relations in my model and I try filter results in grid view I get error:

My code:

model:




class Services extends CActiveRecord

{

    public $serviceName;

    public $businessName;

    public $trade;

    public $place;

    public $street;


    /**

     * Returns the static model of the specified AR class.

     * @return CActiveRecord the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return '{{Services}}';

    }


    /**

     * @return array validation rules for model attributes.

     */

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('createTime, duration, businessId', 'required'),

            array('createTime, updateTime, duration, businessId', 'numerical', 'integerOnly'=>true),

            array('name', 'length', 'max'=>128),

            array('profile', 'safe'),

            

            array('serviceName', 'safe', 'on'=>'search'),

            array('businessName', 'safe', 'on'=>'search'),

            array('trade', 'safe', 'on'=>'search'),

            array('place', 'safe', 'on'=>'search'),

            array('street', 'safe', 'on'=>'search'),

        );

    }


    /**

     * @return array relational rules.

     */

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'business'=>array(self::BELONGS_TO, 'Business', 'businessId'),    

        );

    }


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeLabels()

    {

        return array(

            'id' => 'Id',

            'createTime' => 'Create Time',

            'updateTime' => 'Update Time',

            'name' => 'Name',

            'duration' => 'Duration',

            'profile' => 'Profile',

            'businessId' => 'Business',

        );

    }

    

    public function behaviors(){

        return array(

            'CTimestampBehavior' => array(

                'class' => 'zii.behaviors.CTimestampBehavior',

                'createAttribute' => 'createTime',

                'updateAttribute' => 'updateTime',

                'setUpdateOnCreate' => true,

            )

        );

    }

    

    /**

     * Retrieves a list of models based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */

    public function search()

    {

        $criteria=new CDbCriteria;


        $criteria->with=array(

            'business',

        );

        

        $criteria->compare('name',$this->serviceName);

        $criteria->compare('business.name',$this->businessName);

        $criteria->compare('business.trade',$this->trade);

        $criteria->compare('business.place',$this->place);

        $criteria->compare('business.street',$this->street);


        return new CActiveDataProvider('Services', array(

            'criteria'=>$criteria,

            'pagination'=>array(    // every property of CPagination can be configured here

                'pageSize'=>10,

            ),


        ));

    }

}



view:




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

    'id'=>'services',

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

    'filter'=>$serv,

    'columns'=>array(

        'name',

        'business.name',

        'business.trade',

        'business.place',

        'business.street',

        array(

            'class'=>'CButtonColumn',

        ),

    ),

)); ?>



when I comment filter grid works fine.

Thanks for help!

because services.business.name is undefined …

define it:




class Services extends CActiveRecord

{

    public $name;

    ...

}



But is it possible to get "name" field using relation to business table?

I found solution in this topic.

Could you share your ServicesController code? How are you passing in the value for [color="#FF0000"]$serv[/color]?

Action from controller:




    public function actionSearch()

    {

        $serv=new Services('search');


        if(isset($_GET['Services'])){

            $serv->attributes=$_GET['Services'];

        }

        

        $this->render('search',array('serv'=>$serv));

    }



Maybe this will help GridView Filtering of Relational Data ring-of-relational-data

The error due to your code




 $criteria->compare('business.name',$this->businessName);



Normally, we still compare businessId, so in your view, you can show business.name, but input is still us businessId.

So change your view code to




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

    'id'=>'services',

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

    'filter'=>$serv,

    'columns'=>array(

        'name',

        //'business.name',

	array(

	    'name'=>'businessId',

	    'value'=>'$data->business->name',

	    'filter'=>CHtml::listData($model->findAll(), 'businessId', 'business.name'),

	),

        ...




next, in your search function




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