Yii CGridView 基本使用(三)关联表相关字段搜索

加入 关联表 相关字段的搜索:

     先说一句,我们在这里仅仅谈 ”一对多“ 的关联搜索,首先,不要忘了我们的数据库,忘记的同学请戳这里:这里。能够看到在 tbl_post 中是有一个外键关联到 tbl_user 表的,用以查找作者的相关信息。建了数据库之后,看看我们生成的 Yii 代码的 POST 的 Model。 里面的 realtion 例如以下(忽略 comment 的):

	/**
	 * @return array relational rules.
	 */
	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(
			'comments' => array(self::HAS_MANY, 'Comment', 'post_id'),
			'author' => array(self::BELONGS_TO, 'User', 'author_id'),
		);
	}
    能够看到 POST 和 USER 表能够通过 author 键进行訪问,比如: $model->author->nickname,并且 这里是 BELONGS_TO 关系。

    说了这么多,我们的需求到底是什么?....


     产品经理推了推眼镜:”我们要在日志的后台管理界面加一个功能,能够通过作者名称搜索到对应的文章。

这个比較急。今晚就要完毕。“


    淡定淡定,不就是改需求吗。忽略进度要求,我们研究一下到底要做什么。

    事实上非常easy的。不就是在 POST 的 admin 界面中添加一列作者名称。然后能够通过作者名的 模糊搜索 去找到相应日志吗?看看代码。要是通过 作者 id 去搜索不就简单了吗?只是这样确实不太友好...假设是展示作者名字而已不也是非常easy吗?加一个 header 然后 value 是 $data->author->username, 问题是这样仅仅能展示,不能进行搜索...哎,好苦恼。

    淡定淡定,不就是多个搜索吗?来。让我告诉你怎么做。


    首先,我们进入 POST 的 model,在一開始的地方加入一个属性:

class Post extends CActiveRecord
{
    public $name; //加入一个 public 属性,代表作者名
    然后改一下 Model 里面 search 的代码。修改部分都已经加了凝视:

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

		$criteria=new CDbCriteria;

     $criteria->with = array('author');  //加入了和 author 的渴求式载入

		$criteria->compare('post_id',$this->post_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('tags',$this->tags,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('author_id',$this->author_id);

     //这里加入了一个 compare, username 是 User 表的字段,$this->name 是我们加入的属性,true 为模糊搜索
		$criteria->compare('username',$this->name,true);

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


    然后在 view 里面,就是 post 目录的 admin.php ,CGridView 改为以下代码:

<?

php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'post-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( 'post_id', 'title', 'content', 'tags', 'status', 'create_time', 'update_time', 'author_id', /*以下就是加入的代码啊*/ array( 'name'=>'作者名称', 'value'=>'$data->author->username', //定义展示的 value 值 'filter'=>CHtml::activeTextField($model,'name'), //加入搜索 filter ), array( 'class'=>'CButtonColumn', ), ), )); ?

>

    你是不是发现如今有了搜索框可是不起作用呢?哈哈,所以我们说文章要坚持看到最后。我们要做的最后一步。就是在 rule 里面。把 name 属性加入到安全搜索字段中,要不然会被 Yii 觉得是不安全字段而过滤掉的。看,就在以下函数的最后一行,safe 前面多了个 name ....

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, status, author_id', 'required'),
			array('status, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>128),
			array('tags', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('post_id, title, content, tags, status, create_time, update_time, author_id, name', 'safe', 'on'=>'search'),
		);
	}


    这个功能我们已经完毕了~~哪里须要等到晚上~~不要告诉产品经理,先来一集暴漫.....

    (好吧。我承认我写得这么罗嗦是由于文青病又犯了。默默治疗去....)


 

原文地址:https://www.cnblogs.com/zfyouxi/p/5323976.html