php - Yii framework : Display value from relation table in admin form -
i have admin.php created gii, inside there's table column 'lang_id' have relation primary key 'id' of table 'lang'.
what should put in columns array ? think should use "lang.name" didn't work.
protect/view/mainmenu/admin.php
<?php $this->widget('zii.widgets.grid.cgridview', array( 'id'=>'mainmenu-grid', 'dataprovider'=>$model->search(), 'filter'=>$model, 'columns'=>array( 'menu_id', 'lang.name', // want column display name of language, instead of lang_id 'name', 'remark', array( 'class'=>'cbuttoncolumn', 'template'=>'{update}' ), ), )); ?>
protect/model/mainmenu.php
public function relations(){ return array( 'lang'=>array(self::has_one, 'lang', 'lang_id') ); } public function search(){ $criteria=new cdbcriteria; $criteria->compare('id',$this->id,true); $criteria->compare('menu_id',$this->menu_id); $criteria->compare('lang_id',$this->lang_id); $criteria->compare('name',$this->name,true); $criteria->compare('sorting',$this->sorting); $criteria->compare('remark',$this->remark,true); return new cactivedataprovider($this, array( 'criteria'=>$criteria, )); }
the relation name lang
. whereas in columns array have used lang
(with l in uppercase). have this
'lang.name'
in columns array
and second thing in search()
you need add 1 more line
$criteria->with = array( 'lang' );
Comments
Post a Comment