Hi Everyone,
I am having trouble getting the GridView to display filters for relational data. I am trying to manage a TopDestination model, that holds a list of location IDs. The TopDestination model is related to a Location model as follows:
TopDestination Model:
Location Model:
I have created the admin view to manage TopDestinations, and I have added the Location data to the GridView as follows:
Admin View:
Here is how the admin function looks in my TopDestination Controller:
TopDestination Controller:
And here is the search function in the TopDestination Model:
TopDestination Model:
I cannot get the GridView to display filters for any of the Location data. Only the filter for location_id displays.
Does anyone know what I am missing? I have a feeling it is in the search criteria, but I am unable to get any criteria to work for the fields coming from the Location model!
Your help is much appreciated
I am having trouble getting the GridView to display filters for relational data. I am trying to manage a TopDestination model, that holds a list of location IDs. The TopDestination model is related to a Location model as follows:
TopDestination Model:
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( 'location' => array(self::BELONGS_TO, 'Location', 'location_id'), ); }
Location Model:
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( 'carHireSearchSessions' => array(self::HAS_MANY, 'CarHireSearchSession', 'pick_up_location_id'), 'carHireSearchSessions1' => array(self::HAS_MANY, 'CarHireSearchSession', 'drop_off_location_id'), 'topAirport' => array(self::HAS_ONE, 'TopAirport', 'location_id'), 'topDestination' => array(self::HAS_ONE, 'TopDestination', 'location_id'), ); }
I have created the admin view to manage TopDestinations, and I have added the Location data to the GridView as follows:
Admin View:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'top-destination-grid', 'dataProvider'=>$model->search(), 'filter' => $model, 'columns'=>array( array( 'name' => 'location_id', ), array( 'name'=>'location.location_title', 'value'=>'$data->location->location_title', 'filter'=>CHtml::listData(Location::model()->findAll(), 'id', 'location_title'), 'htmlOptions'=>array('width'=>'90px'), ), array( 'name' => 'location.city_title', ), array( 'name' => 'location.airport_code', ), array( 'class'=>'CButtonColumn', ), ), )); ?>
Here is how the admin function looks in my TopDestination Controller:
TopDestination Controller:
public function actionAdmin() { //$locations = TopDestination::model()->with('location')->search(); $model=new TopDestination('search'); //$locations = TopDestination::model()->with('location')->findByPK($model->location_id); // Only show the active records $model = $model->active(); $model->unsetAttributes(); // clear any default values //$model->with('location'); //fb($locations); if(isset($_GET['TopDestination'])) $model->attributes=$_GET['TopDestination']; $this->render('admin',array( 'model'=>$model, //'locations'=>$locations, )); }
And here is the search function in the TopDestination Model:
TopDestination Model:
public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; fb($this); $criteria->compare('t.location_id',$this->location_id,true); $criteria->with = 'location'; $criteria->compare('location.location_title', $this->location); $criteria->addSearchCondition("location.location_title",$this->location->location_title); // This line does not work - 'trying to get property of non object' error $criteria->together = TRUE; return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); }
I cannot get the GridView to display filters for any of the Location data. Only the filter for location_id displays.
Does anyone know what I am missing? I have a feeling it is in the search criteria, but I am unable to get any criteria to work for the fields coming from the Location model!
Your help is much appreciated
