Yii framework : Display value from relation table in admin form - php

I have a admin.php created by gii, inside it there's a table column 'lang_id' have relation to primary key 'id' of table 'lang'.
What should I put in the columns array ? I think i should use "Lang.name" but it 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', // I want this column display the 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 is lang. Whereas in the columns array you have used Lang (with l in uppercase).
So you will have to do this
'lang.name'
in the columns array
and second thing in the search()
You need to add one more line
$criteria->with = array(
'lang'
);

maybe this can work
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
array(
'name'=>'Lang',
'value'=>'$data->lang->name',
),
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>

Related

Searching relations in Yii

Im having problems in the CGridView when Im trying to search a value that its related to another table (model).
My Relations are:
public function relations()
{
return array(
'iduserFrom' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_from'),
'iduserTo' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_to'),
);
}
And my search():
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('idinvitation',$this->idinvitation);
$criteria->compare('iduser_from',$this->iduser_from);
$criteria->compare('iduser_to',$this->iduser_to);
$criteria->compare('state',$this->state);
$criteria->compare('create_time',$this->create_time,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and in case is necessary,
My CGridView:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'invitation-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'idinvitation',
array(
'name' => 'iduser_from',
'value' => 'CrugeStoredUser::Model()->FindByPk($data->iduser_from)->username',
),
array(
'name' => 'iduser_to',
'value' => 'CrugeStoredUser::Model()->FindByPk($data->iduser_to)->username',
),
array(
'name' => 'state',
'value'=>'Invitation::getEstadoInvitacion($data->state)',
'filter'=>CHtml::dropDownList(
'Invitation[state]',
$model->state,
CHtml::listData(Invitation::model()->getEstadoLista(), 'id', 'valor'),
array(
'empty' => 'Todos',
)
),
'cssClassExpression'=> 'Invitation::getEstadoColor($data->state)',
),
'create_time',
array(
'class'=>'CButtonColumn',
),
),)); ?>
So what Im trying is searching an user by his "username" located in model "CrugeStoredUser", but when I try this I get nothing
Some Help?
I've just spent few days over the same problem.
My solution is
Model:
public $iduser_from_name;
public $iduser_to_name;
public function rules()
{
return array(
...
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('...., iduser_from_name,iduser_to_name ', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
...
'iduser_from_name ' => 'Username From',
'iduser_to_name ' => 'Username To',
);
}
public function search()
{
...
if($this->iduser_from_name)
{
$criteria->together = true;
$criteria->with = array('iduserFrom');
$criteria->compare('iduserFrom.username',$this->iduser_from_name,true);
}
if($this->iduser_to_name)
{
$criteria->together = true;
$criteria->with = array('iduserTo');
$criteria->compare('iduserTo.username',$this->iduser_to_name,true);
}
...
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'invitation-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'iduser_from_name',
'value'=>'$data->iduserFrom->username',
),
array(
'name'=>'iduser_to_name',
'value'=>'$data->iduserTo->username',
),
.....
));
Please make sure following points:
--->Make new "property" (required)
--->mark as "safe" in 'on'=>'search' scenario in rules method (required)
--->set name in attributeLabels method (optional)
--->add condition with "with" and "together" in search function (required)
--->set "name" in "columns" as the "property" name(required
I've tested it with MANY_MANY realtion but I'm qite sure it works with other types.

How to lesser text description in CGridView in Yii

I have that code in my admin.php i.e. view file in my Yii Project.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'description:html', // I want to change this
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Actually description:html is showing a lot of text description which is stored in my database.
So my question is that, I want to print just a single line of description here.
So I don't know how to accomplish it.
Any help will be appreciated.
Thanks.
Although hett has given an answer but you can do this also
array(
'name'=>'description',
'value'=>array($this,'showFewLines')
),
and then in your controller create a function
public function showFewLines($data,$row)
{
$allData=$data->description;
return substr($allData, 0, 40);
}
somehow
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'name'=>'description',
'value'=>function($data) {
return substr($data->description, 0, strpos($data->description, '<br>'));
},
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>

Yii use ItemAlias in CGridView

I am using Yii's 'ItemAlias' function to give a custom name to a value, like this:
<?php
public static function itemAlias($type,$code=NULL) {
$_items = array(
'currency' => array(
'1'=>'US Dollar',
'2'=>'Euro',
'3'=>'GB Pound',
'4'=>'Chinese RMB',
'5'=>'Singaporese Dollar'
)
);
if (isset($code))
return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
else
return isset($_items[$type]) ? $_items[$type] : false;
}
?>
Now, in CGridView, it currently displays the number (key) of the currency which it found in the database, but I want to display it's label. The CGridView is generated like this:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
'currency',
[..etc]
Can someone point me into the right direction to display the label instead of the key of the value? Thanks!
Based off of the limited code you showed this is what I think you want if itemAlias() is a function built into your model:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => '$data->itemAlias("currency",$data->currency)',
),
...
If it is some generic function somewhere not part of your model you would do this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => 'itemAlias("currency",$data->currency)',
),
...

how can i display the result of two tables in gridview display yii

I have two tables Table A and Table B.
I have the pk of table A ,as fk called member_id in table B.
While girdview display of table B, I want to show the "Member name" in table A, using the "member_id" in table B.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
'Location',
...
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
You need to setup a relation to that table, and you can use the relation to reference that field like this:
$data->member->name;
In your transaction model you would place something like:
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(
'member' => array(self::BELONGS_TO, 'Member', 'member_id'),// foreign key
);
}
and for grid you would do like:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'header' => 'Member',
'name' => 'member_id',
'value' => '$data->member->name'
),
'Location',
...
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));

Yii CGridView and SQL Count

I'm trying to load a cGridView with the results of a query between two tables (charity and votes).
Trying to show the number of votes in the vote table for a charity. The vote table has a FK to the charity table.
I could do this in SQL with a left join, but cGridView requires a CActiveDataProvider object to display the data and I am unsure how I can join the two tables to return a result that not only counts, but also doesn't show any results that are equal to 0 and ordered by the votes.
I currently am doing:
in the Vote Model:
public function relations()
{
return array(
'voteCount'=>array(self::STAT, 'Vote', 'charity_id'),
);
}
charity_id being the FK for the charity table.
Then to build the CGridView widget:
$criteria=new CDbCriteria(array(
'with' => 'voteCount',
));
$dataProvider=new CActiveDataProvider('Charity', array(
'pagination' => false,
'criteria' => $criteria,
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
array(
'name'=>'vote.voteCount',
'value'=>'CHtml::encode($data->voteCount)',
),
),
));
Right now it's returning multiple results and I can't seem to figure out how to sort and add a where clause as well.
Any help?
Try add to Charity model
public function relations()
{
return array(
'vote'=>array(self::HAS_ONE, 'Vote', 'charity_id'),
);
}
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
array(
'name'=>'Vote Count',
'value'=>'CHtml::encode($data->vote->voteCount)',
),
),
));
You need some minor changes in
Vote.php (Vote Model)
public function relations()
{
return array(
'voteCount'=>array(self::STAT, 'Vote', 'charity_id'),
);
}
Add a field in attributeLabels()
'voteCount'=>'Votes',
and In CGridView add the 'Votes' column
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
'Votes',
),
));

Categories