Yii中CGridView關(guān)聯(lián)表搜索排序方法實(shí)例詳解
本文實(shí)例講述了Yii中CGridView關(guān)聯(lián)表搜索排序方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
在Yii CGridView 關(guān)聯(lián)表搜索排序?qū)崿F(xiàn)方法有點(diǎn)復(fù)雜,今天看了一老外寫的了篇游戲,下面我整理一下與各位朋友分享一下,相信會(huì)對(duì)大家Yii框架的學(xué)習(xí)有所幫助。
首先,檢查你的blog demo里的protectedmodelsComment.php,確保Comment模型有一個(gè)search的方法,如果沒有,就用gii生成一個(gè),我下載到的blog demo里倒是沒有。
然后,寫代碼的時(shí)間到了,我們從 CommentController 開始,我們給它加一個(gè) actionList:
{
$model=new Comment('search');
$model->unsetAttributes();
if(isset($_GET['Comment']))
$model->attributes=$_GET['Comment'];
$this->render('list',array(
'model'=>$model,
));
}
著看起來(lái)沒什么了不起的,跟你用gii生成的crud代碼里的一樣?,F(xiàn)在讓我來(lái)創(chuàng)建view,在 /protected/views/comment/ 目錄下創(chuàng)建list.php然后粘貼以下代碼
'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'
),
));
?>
Comment List
這是一個(gè)基本的 CGridView 只顯示評(píng)論的‘content', ‘status' and ‘a(chǎn)uthor', 和文章的標(biāo)題。我們假設(shè)想要往這張list里添加一列文章的標(biāo)題,我們只需要添加post.title 就行了:
'content',
'post.title',
'status',
'author',
),
現(xiàn)在如果你訪問以下這個(gè)頁(yè)面,發(fā)現(xiàn)文章的標(biāo)題的確顯示出來(lái)了
問題:
如果你仔細(xì)瞅瞅這個(gè)頁(yè)面你會(huì)發(fā)現(xiàn)你無(wú)法搜索文章標(biāo)題,你也沒辦法按文章標(biāo)題排序,這是因?yàn)?CGridView 在給定的 column name 里面發(fā)現(xiàn)了一個(gè)‘.',也就是 post.title 的點(diǎn)。如果有點(diǎn)號(hào)的話,它就不會(huì)生成搜索框。
解決方案:
要想解決這個(gè)問題,我們得費(fèi)點(diǎn)力氣。首先我們得給Commen模型添加一個(gè) getter 和一個(gè) setter ,比如說(shuō)這么寫:
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;
}
接下來(lái)將這個(gè)屬性添加到 rules 函數(shù)里:
{
// 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'),
);
}
這還不夠,最需要改動(dòng)的是我們的 search 函數(shù)。首先我們要添一個(gè) criteria:
$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);
然后我們添加排序:
$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',
),
);
你也許注意到了我在使用完整的 ‘tablename'.'columnname'語(yǔ)法,我這么做的原因是為了避免 mysql 拋出‘column is ambigious error'。
為了保證這一切正常運(yùn)行,我們必須傳遞 CSort 實(shí)例和 CDbCriteria 實(shí)例給 CActiveDataProvider :
'criteria'=>$criteria,
'sort'=>$sort
));
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>$sort
));
現(xiàn)在我們要做的就是修改我們的 view 以便它在 CGridView 顯示想要顯示的屬性:
'content',
'postTitle',
'status',
'author',
),
刷新一下,應(yīng)該可以了,效果如下圖所示:
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- yii2使用gridView實(shí)現(xiàn)下拉列表篩選數(shù)據(jù)
- yii2.0之GridView自定義按鈕和鏈接用法
- 淺析Yii2中GridView常見操作
- 淺析Yii2 gridview實(shí)現(xiàn)批量刪除教程
- Yii2 GridView實(shí)現(xiàn)列表頁(yè)直接修改數(shù)據(jù)的方法
- 淺析Yii2 GridView實(shí)現(xiàn)下拉搜索教程
- 淺析Yii2 GridView 日期格式化并實(shí)現(xiàn)日期可搜索教程
- yii2中結(jié)合gridview如何使用modal彈窗實(shí)例代碼詳解
- Yii把CGridView文本框換成下拉框的方法
- yii gridview實(shí)現(xiàn)時(shí)間段篩選功能
相關(guān)文章
php使HTML標(biāo)簽自動(dòng)補(bǔ)全閉合函數(shù)代碼
這個(gè)網(wǎng)上找到的自動(dòng)補(bǔ)全閉合函數(shù)還挺不錯(cuò)的,它可以根據(jù)你的html內(nèi)容自己補(bǔ)全閉合,確保HTMl代碼正確2012-10-10實(shí)例分析基于PHP微信網(wǎng)頁(yè)獲取用戶信息
本篇內(nèi)容主要給大家詳細(xì)分析了用PHP制作微信網(wǎng)頁(yè)來(lái)獲取用戶基本信息的過程,以及步驟講解。2017-11-11openai createChatCompletion函數(shù)使用實(shí)例
這篇文章主要為大家介紹了openai createChatCompletion函數(shù)使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Codeigniter整合Tank Auth權(quán)限類庫(kù)詳解
相交其他CodeIgniter的類庫(kù),tank_auth,配置簡(jiǎn)單,使用也簡(jiǎn)單,并且作者也一直在更新。這篇文章主要介紹了Codeigniter整合Tank Auth權(quán)限類庫(kù)詳解,需要的朋友可以參考下2014-06-06php中使用array_filter()函數(shù)過濾空數(shù)組的實(shí)現(xiàn)代碼
這篇文章主要介紹了php中使用array_filter()函數(shù)過濾空數(shù)組的實(shí)現(xiàn)代碼,這是瀏覽PHP手冊(cè)時(shí)無(wú)意發(fā)意的一個(gè)有意思的array_filter()函數(shù)用法,需要的朋友可以參考下2014-08-08