《PHP實例:Yii中CGridView關聯表搜索排序方法實例詳解》要點:
本文介紹了PHP實例:Yii中CGridView關聯表搜索排序方法實例詳解,希望對您有用。如果有疑問,可以聯系我們。
PHP學習本文實例講述了Yii中CGridView關聯表搜索排序辦法.分享給大家供大家參考.具體實現辦法如下:
PHP學習在Yii CGridView 關聯表搜索排序實現辦法有點復雜,今天看了一老外寫的了篇游戲,下面我整理一下與各位朋友分享一下,相信會對大家Yii框架的學習有所幫助.
PHP學習首先,檢查你的blog demo里的protectedmodelsComment.php,確保Comment模型有一個search的辦法,如果沒有,就用gii生成一個,我下載到的blog demo里倒是沒有.
PHP學習然后,寫代碼的時間到了,我們從 CommentController 開始,我們給它加一個 actionList:
代碼如下:
public function actionList()
{
??? $model=new Comment('search');
??? $model->unsetAttributes();
??? if(isset($_GET['Comment']))
??????? $model->attributes=$_GET['Comment'];
?
??? $this->render('list',array(
??????? 'model'=>$model,
??? ));
}
PHP學習著看起來沒什么了不起的,跟你用gii生成的crud代碼里的一樣.現在讓我來創建view,在 /protected/views/comment/ 目錄下創建list.php然后粘貼以下代碼
代碼如下:
<?php $this->breadcrumbs=array(
??? 'Comments',
);
?>
?
<h1>Manage Comments</h1>
?
<?php $this->widget('zii.widgets.grid.CGridView', array(
??? 'dataProvider'=>$model->search(),
??? 'filter'=>$model,
??? 'columns' => array(
??????????????? 'content',
??????????????? 'post.title',
??????????????? 'status',
??????????????? 'author'
??????? ),
));
?>
PHP學習Comment List
PHP學習這是一個基本的 CGridView 只顯示評論的‘content', ‘status' and ‘author', 和文章的標題.我們假設想要往這張list里添加一列文章的標題,我們只需要添加post.title 就行了:
代碼如下:
'columns'=>array(
??? 'content',
??? 'post.title',
??? 'status',
??? 'author',
),
PHP學習現在如果你拜訪以下這個頁面,發現文章的標題的確顯示出來了
PHP學習![PHP實例:Yii中CGridView關聯表搜索排序方法實例詳解]()
PHP學習問題:
PHP學習如果你仔細瞅瞅這個頁面你會發現你無法搜索文章標題,你也沒方法按文章標題排序,這是因為 CGridView 在給定的 column name 里面發現了一個‘.',也就是 post.title 的點.如果有點號的話,它就不會生成搜索框.
PHP學習辦理方案:
PHP學習要想辦理這個問題,我們得費點力氣.首先我們得給Commen模型添加一個 getter 和一個 setter ,比如說這么寫:
代碼如下:
private $_postTitle = null;
public function getPostTitle()
{
??? if ($this->_postTitle === null && $this->post !== null)
??? {
??????? $this->_postTitle = $this->post->title;
??? }
??? return $this->_postTitle;
}
public function setPostTitle($value)
{
??? $this->_postTitle = $value;
}
PHP學習接下來將這個屬性添加到 rules 函數里:
代碼如下:
public function rules()
{
??? // NOTE: you should only define rules for those attributes that
??? // will receive user inputs.
??? return array(
??????? array('content, author, email', 'required'),
??????? array('author, email, url', 'length', 'max'=>128),
??????? array('email','email'),
??????? array('url','url')
?
??????? array('content, postTitle, status, author', 'safe', 'on'=>'search'),
??? );
}
PHP學習這還不夠,最需要改動的是我們的 search 函數.首先我們要添一個 criteria:
代碼如下:
$criteria=new CDbCriteria;
$criteria->with = "post"; // 確保查詢 post 表
?
$criteria->compare('t.content',$this->content,true);
$criteria->compare('t.status',$this->status);
$criteria->compare('t.author',$this->author,true);
$criteria->compare('post.title', $this->postTitle,true);
PHP學習然后我們添加排序:
代碼如下:
$sort = new CSort();
$sort->attributes = array(
??? 'defaultOrder'=>'t.create_time DESC',
??? 'content'=>array(
??????? 'asc'=>'t.content',
??????? 'desc'=>'t.content desc',
??? ),
??? 'status'=>array(
??????? 'asc'=>'t.status',
??????? 'desc'=>'t.status desc',
??? ),
??? 'author'=>array(
??????? 'asc'=>'t.author',
??????? 'desc'=>'t.author desc',
??? ),
??? 'postTitle'=>array(
??????? 'asc'=>'post.title',
??????? 'desc'=>'post.title desc',
??? ),
);
PHP學習你也許注意到了我在使用完整的 ‘tablename'.'columnname'語法,我這么做的原因是為了避免 mysql 拋出‘column is ambigious error'.
PHP學習為了保證這一切正常運行,我們必須傳遞 CSort 實例和 CDbCriteria 實例給 CActiveDataProvider :
代碼如下:
return new CActiveDataProvider('Comment', array(
??? 'criteria'=>$criteria,
??? 'sort'=>$sort
));
PHP學習return new CActiveDataProvider('Comment', array(
??? 'criteria'=>$criteria,
??? 'sort'=>$sort
));
PHP學習現在我們要做的就是修改我們的 view 以便它在 CGridView 顯示想要顯示的屬性:
代碼如下:
'columns'=>array(
??? 'content',
??? 'postTitle',
??? 'status',
??? 'author',
),
PHP學習刷新一下,應該可以了,效果如下圖所示:
PHP學習![PHP實例:Yii中CGridView關聯表搜索排序方法實例詳解]()
PHP學習希望本文所述對大家基于Yii框架的PHP程序設計有所贊助.
維易PHP培訓學院每天發布《PHP實例:Yii中CGridView關聯表搜索排序方法實例詳解》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.fzlkiss.com/jiaocheng/13491.html