使用CGridView,Yii在BELONGS_TO模型列中搜索 [英] Search in BELONGS_TO model column with CGridView, Yii

查看:51
本文介绍了使用CGridView,Yii在BELONGS_TO模型列中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于Lesson模型的CGridView小部件

I have a CGridView widget for Lesson model

$this->widget('zii.widgets.grid.CGridView', array(
     'id'=>'lesson-grid',
     'dataProvider'=>$model->search(),
     'filter'=>$model,

...并且Lesson与用户模型有关:

... and Lesson has relation to the User model:

'user' => array(self::BELONGS_TO, 'User', 'user_id'),

...,并且CGridView有一列,其中包含上述BELONGS_TO模型中用户的姓氏

... and the CGridView has a column with user's lastname from the BELONGS_TO model described above

'columns'=>array(
        ...
        array(
            'name' => 'user',
            'header'=>'Teacher',
            'value' => '$data->user->lastname',
        ),

因此,我不能在本专栏中用CGridView进行搜索,但是我需要它.

So, I can't symply search with CGridView in this column, but I need it.

如何使用CGridView搜索"$ data-> user-> secondname"?

How to search in '$data->user->secondname' with CGridView?

我认为我应该在Lesson模型中扩展搜索方法,但是如何?

I think that I should extend search method in Lesson model, but how?

现在看起来像这样:

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('student',$this->student,true);
    $criteria->compare('comment',$this->comment,true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
    ));
}

推荐答案

这应该可行,将其添加到search()方法中的搜索条件中:

This should work, add it to your search criteria in the search() method:

$criteria->with[]='user';
$criteria->addSearchCondition("user.secondname",$this->user_id);

这是我要做的:

if(!intval($this->user_id) && is_string($this->user_id) && strlen($this->user_id) > 0) {
  $criteria->with[]='user';
  $criteria->addSearchCondition("user.secondname",$this->user_id);
} else
  $criteria->compare('t.user_id',$this->user_id);

这是CGridView定义:

And here is the CGridView definition:

'columns'=>array(
  ...
  array(
    'name' => 'user_id',
    'header'=>'User',
    'sortable'=>false, // since it would still be sorting based on ID
    // 'value' => '$data->user->lastname', // basic version
    'value'=>'CHtml::link((isset($data->user))?$data->user->username:$data->user_id,array("user/view","id"=>$data->user_id))', // link version
    ),

这是一个有趣的小技巧:如果搜索项是字符串而不是intval(),它将通过用户名通过用户的第二名来搜索用户.但是,如果输入user_id,它将通过用户的user_id(默认的search()功能)找到用户.

It's a fun little trick: if the search term is a string and NOT an intval(), it searches for the user by their secondname, via the "user" relation. But if you enter a user_id, it will find the user by their user_id - the default search() functionality.

注意:这将启用过滤功能,但仍会根据ID进行排序.您将需要实施一些其他操作才能使排序工作正常进行.

NOTE: This will enable filtering, but it will still sort based on ID. You will need to implement something additional to get the sorting to work.

当然还有其他方法可以做到这一点,但这就是我的方法.我怀疑使用该关系执行此操作是正确的",但是我的技术工作可靠.

There are surely other ways to do this, but this is how I do it. I suspect there is a "right" to do this using the relation, but my technique works solid.

这篇关于使用CGridView,Yii在BELONGS_TO模型列中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆