php - Yii, relation model filter serach -


i battling cgridview filtering using related model column, have 2 models cars , colour, colour has_many car , car belongs_to colour. grid column related model displays fine, i'm not able filter it. getting mysql error

column not found: 1054 unknown column 'carname' 

colour model has_many - cars

i declaired variable $carname car model, model can see it.

public $carname; public function rules() {    return array(           array('id, carname, colourname', 'safe', 'on'=>'search'),                ) } public function relations() {      return array(                 'carsobj'=>array(self::has_many, 'cars', 'colourid')     ); }  public function search() {       $criteria=new cdbcriteria;     $criteria->with = "carsobj";     $criteria->compare('carname', $this->carname, true);      $criteria->compare('id',$this->id);     $criteria->compare('colourname',$this->colourname,true);       return new cactivedataprovider($this, array(         'criteria'=>$criteria,     )); } 

car model belongs_to - colour

public function relations() {   return array(     'colourobj'=>array(self::belongs_to, 'colour', 'colourid')  ); } 

cgridview, using colour model search dataprovider

 $model = new colour('search');  $data = $model->search();   $this->widget('zii.widgets.grid.cgridview', array(            'dataprovider'=>$data            ,'filter'=>$model             ,'pager'=>array('header'=>'')              ,'columns'=>array( //related model column                     'id',                      'colourname',                       array(                             'header' => 'carname',                             'type' => 'raw',                              'name' => 'carname',                             'value' => function($data, $row) { //$data item of dataprovider, $row number of row (starts 0)                                 $carnames= chtml::listdata((array)$data->colourobj, 'id', 'carname');                                 return implode(', ', $carnames);                             }                             ) 

by default cactiverecord using lazy load. need set attribute together true. it's practice set table alias when using with.

public function search(){      $criteria=new cdbcriteria;     $criteria->together = true;     $criteria->with = 'carsobj';     $criteria->compare('carsobj.carname',$this->carname,true);     $criteria->compare('t.id',$this->id);     $criteria->compare('t.colourname',$this->colourname,true);      return new cactivedataprovider($this, array(         'criteria'=>$criteria,     )); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -