Yii中CGridView关联表搜索排序方法实例详解


Posted in PHP onDecember 03, 2014

本文实例讲述了Yii中CGridView关联表搜索排序方法。分享给大家供大家参考。具体实现方法如下:

在Yii CGridView 关联表搜索排序实现方法有点复杂,今天看了一老外写的了篇游戏,下面我整理一下与各位朋友分享一下,相信会对大家Yii框架的学习有所帮助。

首先,检查你的blog demo里的protectedmodelsComment.php,确保Comment模型有一个search的方法,如果没有,就用gii生成一个,我下载到的blog demo里倒是没有。

然后,写代码的时间到了,我们从 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,

    ));

}

着看起来没什么了不起的,跟你用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' 

        ),

));

?>

Comment List

这是一个基本的 CGridView 只显示评论的‘content', ‘status' and ‘author', 和文章的标题。我们假设想要往这张list里添加一列文章的标题,我们只需要添加post.title 就行了:

'columns'=>array(

    'content',

    'post.title',

    'status',

    'author',

),

现在如果你访问以下这个页面,发现文章的标题的确显示出来了

Yii中CGridView关联表搜索排序方法实例详解

问题:

如果你仔细瞅瞅这个页面你会发现你无法搜索文章标题,你也没办法按文章标题排序,这是因为 CGridView 在给定的 column name 里面发现了一个‘.',也就是 post.title 的点。如果有点号的话,它就不会生成搜索框。

解决方案:

要想解决这个问题,我们得费点力气。首先我们得给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;

}

接下来将这个属性添加到 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'),

    );

}

这还不够,最需要改动的是我们的 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);

然后我们添加排序:

$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',

    ),

);

你也许注意到了我在使用完整的 ‘tablename'.'columnname'语法,我这么做的原因是为了避免 mysql 抛出‘column is ambigious error'。

为了保证这一切正常运行,我们必须传递 CSort 实例和 CDbCriteria 实例给 CActiveDataProvider :

return new CActiveDataProvider('Comment', array(

    'criteria'=>$criteria,

    'sort'=>$sort

));
return new CActiveDataProvider('Comment', array(

    'criteria'=>$criteria,

    'sort'=>$sort

));

现在我们要做的就是修改我们的 view 以便它在 CGridView 显示想要显示的属性:

'columns'=>array(

    'content',

    'postTitle',

    'status',

    'author',

),

刷新一下,应该可以了,效果如下图所示:

Yii中CGridView关联表搜索排序方法实例详解

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php字符串截取问题
Nov 28 PHP
关于Appserv无法打开localhost问题的解决方法
Oct 16 PHP
PHP定时执行计划任务的多种方法小结
Dec 19 PHP
PHP中使用crypt()实现用户身份验证的代码
Sep 05 PHP
php多功能图片处理类分享(php图片缩放类)
Mar 14 PHP
PHP实现返回JSON和XML的类分享
Jan 28 PHP
基于php判断客户端类型
Oct 14 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
Dec 09 PHP
PHP+jQuery实现即点即改功能示例
Feb 21 PHP
Laravel框架创建路由的方法详解
Sep 04 PHP
浅谈laravel框架与thinkPHP框架的区别
Oct 23 PHP
PHP8.0新功能之Match表达式的使用
Jul 19 PHP
yii实现CheckBox复选框在同一行显示的方法
Dec 03 #PHP
Yii把CGridView文本框换成下拉框的方法
Dec 03 #PHP
Yii实现多按钮保存与提交的方法
Dec 03 #PHP
Yii实现MySQL多数据库和读写分离实例分析
Dec 03 #PHP
Yii框架登录流程分析
Dec 03 #PHP
Yii框架获取当前controlle和action对应id的方法
Dec 03 #PHP
PHP多线程类及用法实例
Dec 03 #PHP
You might like
php 小乘法表实现代码
2009/07/16 PHP
PHP 判断变量类型实现代码
2009/10/23 PHP
php获取bing每日壁纸示例分享
2014/02/25 PHP
PHP会话控制:Session与Cookie详解
2014/09/27 PHP
php通过两层过滤获取留言内容的方法
2016/07/11 PHP
PHP读取大文件的几种方法介绍
2016/10/27 PHP
jquery 缓存问题的几个解决方法
2013/11/11 Javascript
JavaScript中数据结构与算法(一):栈
2015/06/19 Javascript
阻止表单提交按钮多次提交的完美解决方法
2016/05/16 Javascript
原生JS查找元素的方法(推荐)
2016/11/22 Javascript
利用n 升级工具升级Node.js版本及在mac环境下的坑
2017/02/15 Javascript
微信小程序之页面跳转和参数传递的实现
2017/09/29 Javascript
vue中post请求以a=a&amp;b=b 的格式写遇到的问题
2018/04/27 Javascript
NodeJS使用Range请求实现下载功能的方法示例
2018/10/12 NodeJs
ES5新增数组的实现方法
2020/05/12 Javascript
在Python中使用异步Socket编程性能测试
2014/06/25 Python
Python3中bytes类型转换为str类型
2018/09/27 Python
使用PIL(Python-Imaging)反转图像的颜色方法
2019/01/24 Python
详解爬虫被封的问题
2019/04/23 Python
python点击鼠标获取坐标(Graphics)
2019/08/10 Python
pandas-resample按时间聚合实例
2019/12/27 Python
PyCharm 2020 激活到 2100 年的教程
2020/03/25 Python
python自动化测试三部曲之unittest框架的实现
2020/10/07 Python
澳大利亚在线床零售商:Bedworks
2020/09/01 全球购物
计算机专业大学生的自我评价
2013/11/14 职场文书
工地资料员岗位职责
2013/12/31 职场文书
医学专业应届生的自我评价
2014/02/28 职场文书
中医学专业自荐信范文
2014/04/01 职场文书
省文明单位申报材料
2014/05/08 职场文书
市政工程技术专业自荐书
2014/07/06 职场文书
放飞梦想演讲稿800字
2014/08/26 职场文书
2014年十八届四中全会思想汇报范文
2014/10/17 职场文书
总经理年会致辞
2015/07/29 职场文书
实习报告怎么写
2019/06/20 职场文书
教你怎么用python selenium实现自动化测试
2021/05/27 Python
python中subplot大小的设置步骤
2021/06/28 Python