Yii CGridView 基本使用(三)關聯表相關欄位搜尋,yiicgridview
添加 關聯表 相關欄位的搜尋:
先說一句,我們在這裡只談 ”一對多“ 的關聯搜尋,首先,不要忘了我們的資料庫,忘記的同學請戳這裡:這裡,可以看到在 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'),);}
這個功能我們已經完成了~~哪裡需要等到晚上~~不要告訴產品經理,先來一集暴漫.....
(好吧,我承認我寫得這麼羅嗦是因為文青病又犯了,默默治療去....)
yii CGridView click事件
array(
'header' => '查看音頻',
//....
'buttons' => array(
'play' => array(
'label' => '播放',
'imageUrl' => Yii::app()->request->baseUrl . '/images/play.jpg',
'url' => 'Yii::app()->createUrl("/yin/play", array("wavpath"=>$data->wav_path))',
'click' => 'noBorderWin(this.src,300,200,"#000000","#CCCCCC","音頻播放","#000000","no");return false;',
),
)
);
gridview對$data引用是即時解析的(用eval()來執行),所以要以字串形式儲存(因此要求使用單引號包裹字串),直接寫肯定會報變數未定義。
希望對你有協助
參考資料:www.yiiframework.com/doc/api/1.1/CGridView
yii架構中的CGridView 問題
增加sort欄位