Yii CGridView 关联表搜索排序实例
在这篇文章中,我准备讲解如何在CGridView中搜索或者排序关联表中的某一行,通过给Yii Blog demo添加一个list页面。
首先,检查你的blog demo里的protected\models\Comment.php,确保Comment模型有一个search的方法,如果没有,就用gii生成一个,我下载到的blog demo里倒是没有。
然后,写代码的时间到了,我们从 CommentController 开始,我们给它加一个 actionList:
|
1
2
3
4
5
6
7
8
9
10
11
|
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然后粘贴以下代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?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 就行了:
|
1
2
3
4
5
6
|
'columns'=>array( 'content', 'post.title', 'status', 'author',), |
现在如果你访问以下这个页面,发现文章的标题的确显示出来了

问题
如果你仔细瞅瞅这个页面你会发现你无法搜索文章标题,你也没办法按文章标题排序,这是因为 CGridView 在给定的 column name 里面发现了一个‘.’,也就是 post.title 的点。如果有点号的话,它就不会生成搜索框。
解决方案
要想解决这个问题,我们得费点力气。首先我们得给Commen模型添加一个 getter 和一个 setter ,比如说这么写:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 函数里:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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:
|
1
2
3
4
5
6
7
|
$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); |
然后我们添加排序:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$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 :
|
1
2
3
4
|
return new CActiveDataProvider('Comment', array( 'criteria'=>$criteria, 'sort'=>$sort)); |
现在我们要做的就是修改我们的 view 以便它在 CGridView 显示想要显示的属性:
|
1
2
3
4
5
6
|
'columns'=>array( 'content', 'postTitle', 'status', 'author',), |
刷新一下,应该可以了:

感觉还不错:-D。你可以自由地分享这篇文章,hankcs翻译自:
Yii CGridView 关联表搜索排序实例的更多相关文章
- Yii 1开发日记 -- 后台搜索功能下拉及关联表搜索
Yii 1 实现后台搜索,效果如下: 一. 下拉搜索: 1.模型中和常规的一样 if (isset($_GET['agency']['status']) && $_GET['agenc ...
- Yii CGridView 基本使用(三)关联表相关字段搜索
加入 关联表 相关字段的搜索: 先说一句,我们在这里仅仅谈 "一对多" 的关联搜索,首先,不要忘了我们的数据库,忘记的同学请戳这里:这里.能够看到在 tbl_post 中是有一个外 ...
- YII关联字段并带搜索排序功能
1.简介 从接触yii框架到现在已经快有两个月了,但是自己对yii框架的了解程度并不是很深,并没有系统地去学习,仅仅只是在做项目的时候遇到不懂得知识才去翻手册. 在上一个项目中因为需要将关联的表的字段 ...
- Yii框架 多表查询实例
Yii框架多表查询实例:总共分为两个步骤(以下的代码我全部都写在model中):1.先在主表model中声明关联表中所需要查询的字段. public $surveyls_description; // ...
- Yii 三表关联 角色表、角色权限连接表、权限表
Yii 三表关联 角色表.角色权限连接表.权限表 角色表 role----------------id 唯一序号name 角色名称---------------- 角色权限连接表 lp-------- ...
- 后盾网lavarel视频项目---lavarel多表关联一对多操作实例
后盾网lavarel视频项目---lavarel多表关联一对多操作实例 一.总结 一句话总结: 1.一对多中多那个部分的数据前端通过json弄到服务器 2.所有通过一操作多的时候,都要用上模型中定义的 ...
- EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载
之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...
- AppBox升级进行时 - 关联表查询与更新(Entity Framework)
AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 关联表的查询操作 使用 Include 方法,我们可以在一次数据库查询中将关联 ...
- 搜索实时个性化模型——基于FTRL和个性化推荐的搜索排序优化
本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...
随机推荐
- 聊聊jvm系列
http://blog.csdn.net/column/details/talk-about-jvm.html
- cms的使用与总结
1,把cms中的basecms复制进Wamp里面的www文件夹, 2,打开Wamp,打开网址http://localhost/basecms/core/admin/admin.php(该网址默认端口为 ...
- 【小程序云开发入门】quickStart
开发者可以使用云开发开发微信小程序.小游戏,无需搭建服务器,即可使用云端能力. 云开发为开发者提供完整的云端支持,弱化后端和运维概念,无需搭建服务器,使用平台提供的 API 进行核心业务开发,即可实现 ...
- Storm系列一: Storm初步
初入Storm 前言 学习Storm已经有两周左右的时间,但是认真来说学习过程确实是零零散散,遇到问题去百度一下,找到新概念再次学习,在这样的一个循环又不成体系的过程中不断学习Storm. 前人栽树, ...
- 【树】Lowest Common Ancestor of a Binary Tree(递归)
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...
- Spark源码的编译过程详细解读(各版本)(博主推荐)
不多说,直接上干货! 说在前面的话 重新试多几次.编译过程中会出现下载某个包的时间太久,这是由于连接网站的过程中会出现假死,按ctrl+c,重新运行编译命令. 如果出现缺少了某个文件的情况,则要 ...
- javascript数组原型方法
1.javascript数组原型方法. <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- manjaro开启sdd trim
$ sudo systemctl enable fstrim.timer; $ sudo systemctl start fstrim.service; $ sudo systemctl stat ...
- 面试题22:有序数组生成不同结构BST
对于一个含有n个数的有序数组1~N,能够产生多少种不同结果的二叉搜素树BST? 如何生成这些不同结构的BST? 有序数组如何生成平衡二叉搜索树? class Solution { public: in ...
- Week4——结对练习&团队作业1
Deadline: 2017-10-14 10:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分(结对代码-10分,结对博客-10分,团队博客-10分),检查的项目包括后文的三个方面 按要 ...