Reputation: 55
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?
Upvotes: 1
Views: 471
Reputation: 180
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.
Upvotes: 2