Hello everyone,
I’m trying to get a filter on a relational field in the CGridView.
But the filter wont work i get an error on $this->_country = $this->products->Country;
The error is: Trying to get property of non-object
So $this->products->Country must be undefined.
I got this code from some other questions on this forum. Can anyone tell me what is it I’m missing?
In the model:
private $_country = null;
public function getCountry(){
if($this->_country === null && $this->products_Id !== null) {
$this->_country = $this->products->Country;
}
return $this->_country;
}
public function setCountry($value){
$this->_country = $value;
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('products'=>array('alias'=>'products'));
$criteria->compare('Id',$this->Id);
$criteria->compare('source',$this->source,true);
$criteria->compare('Uploaded',$this->Uploaded,true);
$criteria->compare('products_Id',$this->products_Id);
$criteria->compare('products.Country',$this->country);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
In the view:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'images-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'source',
'Uploaded',
'products.EAN',
'products.Productname',
array(
'header'=>'Merk',
'name'=>'products.brands.Brand',
'filter'=>CHtml::listData(
Brands::model()->findAll(array('order' => 'Brand')),
'Brand','Brand'
),
'value'=>'$data->products->brands->Brand',
),
array(
'header'=>'Landcode',
'name'=>'Country',
'filter'=>CHtml::listData(Products::model()->findAll(array('order'=>'Country')),'Id','Country'),
'value'=>'$data->products->Country',
),
'products_Id',
array(
'class'=>'CButtonColumn',
),
),
));
?>