在Yii中搜索关系

时间:2015-05-20 09:57:52

标签: php yii

当我试图搜索与另一个表(模型)相关的值时,我在CGridView中遇到问题。

我的关系是:

public function relations()
{
    return array(
        'iduserFrom' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_from'),
        'iduserTo' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_to'),
    );
}

我的搜索():

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,
    ));
}

如有必要, 我的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',
    ),
),)); ?>

所以我尝试通过位于模型“CrugeStoredUser”中的“用户名”搜索用户,但是当我尝试这个时我什么都没得到

一些帮助?

1 个答案:

答案 0 :(得分:1)

我刚刚在同一个问题上花了几天时间。 我的解决方案是

型号:

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,
    ));
}

查看:

 $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',
            ),
            .....
));

请确保以下几点:

--->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

我已经用MANY_MANY实现测试了它,但我确信它适用于其他类型。