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 關係。

    說了這麼多,我們的需求究竟是什麼?....


     產品經理推了推眼鏡:”我們要在日誌的後臺管理界面加一個功能,可以通過作者名稱搜索到相應的文章。這個比較急,今晚就要完成。“


    淡定淡定,不就是改需求嗎。忽略進度要求,我們研究一下究竟要做什麼。

    其實很簡單的,不就是在 POST 的 admin 界面中增加一列作者名稱,然後可以通過作者名的 模糊搜索 去找到對應日誌嗎?看看代碼,要是通過 作者 id 去搜索不就簡單了嗎?不過這樣確實不太友好...如果是展示作者名字而已不也是很簡單嗎?加一個 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'),
		);
	}


    這個功能我們已經完成了~~哪裏需要等到晚上~~不要告訴產品經理,先來一集暴漫.....

    (好吧,我承認我寫得這麼羅嗦是因爲文青病又犯了,默默治療去....)


 

發佈了123 篇原創文章 · 獲贊 268 · 訪問量 111萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章