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!