Yii中的relations方法
以Blog示例: 重点看注释
User类中的relations方法如下
- <span style="font-size:18px;background-color: rgb(255, 255, 255);"> public function relations()
- {
- return array(
- 'posts' => array(self::HAS_MANY, 'Post', 'author_id',
- 'order'=>'posts.update_time DESC',
- 'with'=>'comments:approved', // $user = User::model()->findByPk(1); 这里也查出了每篇post所带的comments
- //approved是comment的命名空间,可以在这里设置
- //'together'=>false, 设置这一项,关联查新将被分为几个SQL语句执行,和性能有关系
- ),
- 'postCount'=>array(
- self::STAT,'Post','author_id',
- 'condition'=>'status='.Post::STATUS_PUBLISHED,
- ),
- );
- }</span>
Post中的方法如下 :
- <span style="font-size:18px;background-color: rgb(255, 255, 255);">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(
- 'author'=>array(self::BELONGS_TO,'User','author_id',
- //'select'=>'id,username,profile', // 关联查询的选项,如果不设置,则默认为*即整个关联记录
- ),
- 'comments'=>array(self::HAS_MANY,'Comment','post_id',
- 'condition'=>'comments.status='.Comment::STATUS_APPROVED,
- 'order'=>'comments.create_time DESC'),
- 'commentCount'=>array(
- self::STAT,'Comment','post_id',
- 'condition'=>'status='.Comment::STATUS_APPROVED
- ),
- );
- }
- </span>
Comment中的ralations方法如下:
- <span style="font-size:18px;background-color: rgb(255, 255, 255);">public function attributeLabels() //名字和现实标签的映射数组
- {
- return array(
- 'id' => 'Id',
- 'content' => 'Comment',
- 'status' => 'Status',
- 'create_time' => 'Create Time',
- 'author' => 'Name',
- 'email' => 'Email',
- 'url' => 'Website',
- 'post_id' => 'PostID', //对应的博客ID
- );
- }
- </span>
在控制器中写个方法测试一下结果:
- <span style="font-size:18px;background-color: rgb(255, 255, 255);"> public function actionRQ(){
- $post = Post::model()->find('id=:id',array(':id'=>7));
- echo $post->author->username;
- echo "<hr>";
- $posts = Post::model()->with('author','comments')->findAll(); //急切加载
- foreach($posts as $post){
- echo $post->id." |";
- foreach($post->comments as $comment){
- echo $comment->id.": ";
- echo $comment->content."<br>";
- }
- echo $post->author->username."<br>";
- }
- echo "<hr>";
- $user = User::model()->with('posts.comments')->findByPk(1);
- //$user = User::model()->findByPk(1); 这一句和上一句是一样的,因为在User的relations声明的posts也已经加上了关联查询:with=>'comments'
- foreach($user->posts as $post){ //嵌套急切加载
- echo $post->title." (";
- foreach($post->comments as $comment){
- echo $comment->id." : ";
- echo $comment->content."<br>";
- }
- echo ")".$post->tags."<br>";
- }
- echo "<hr>";
- $criteria = new CDbCriteria;
- //$criteria->select = "username";
- //$criteria->order
- //$criteria->limit
- //$criteria->condition
- //$criteria->params
- $criteria->with = array(
- 'posts.comments',
- );
- $users = User::model()->findAll($criteria);
- foreach($users as $user){
- echo $user->username.":<br>";
- //echo $user->password;
- foreach($user->posts as $post){ //嵌套急切加载
- echo $post->title." (";
- foreach($post->comments as $comment){
- echo $comment->id." : ";
- echo $comment->content."<br>";
- }
- echo ")".$post->tags."<br>";
- }
- }
- //动态关联查询,也就是在查询的时候覆盖relations中设置的关联的选项
- echo "<hr>";
- $user=User::model()->with(array(
- 'posts'=>array(
- 'order'=>'posts.update_time DESC',
- 'with'=>array('comments'=>array('order'=>'comments.id ASC')),
- //'together'=>false, //关联声明中设置together 选项为false 以便一些表被连接在单独的SQL语句中,分为几个SQL语句执行
- ),
- ))->findByPk(1);
- echo "demo 的posts数量为:".$user->postCount;
- echo "<br>";
- foreach($user->posts as $post){
- echo $post->id."(";
- echo $post->title."的评论数量是:".$post->commentCount."<br>";
- foreach($post->comments as $comment){
- echo $comment->id." | ";
- }
- echo ")";
- echo "<br>";
- }
- }</span>
- 原文来自 http://blog.csdn.net/littlebearwmx/article/details/8561018
Yii中的relations方法的更多相关文章
- C#中DataTable中的Compute方法使用收集
原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...
- Yii中的错误及异常处理
Yii中的错误及异常处理 Yii已经默认已经在CApplication上实现了异常和错误的接管,这是通过php的set_exception_handler, set_error_handler实现的. ...
- Yii中事件触发机制
控制器初始化中添加事件处理方法,在需要触发的地方直接触发 public function init() { parent::init(); // TODO: Change the autogenera ...
- 在yii中使用分页
yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...
- yii中的自定义组件
yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...
- YII中的表单挂件
利用助手(widget)在页面实现表单 控制器中 <?php class YiiFormController extends Controller { public function actio ...
- Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)
英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...
- yii 中设置提示成功信息,错误提示信息,警告信息
方法一: <?php Yii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlas ...
- 解密yii中CModule::_components和CModule::_componentConfig
array CModule::_components 所有组件对象(CComponent的子类)将作为键值存在该数组中, 键名是定义该组件时使用的键名.例如: protected function r ...
随机推荐
- 互联网轻量级框架SSM-查缺补漏第二天
简言:第一天没咋看,因为看的时候已经是下午了.今天上午也因为工作上的事没咋看,本来想按照天去写的,但是内容会太散吧.我决定把整块的内容放在一起写了.天数啥的,就那样把. 还有,我只是言简意赅的去总结一 ...
- class path resource [logback.xml] cannot be resolved to URL because it does not exist 问题解决
今天在自己搭建Springboot 框架的时候,在配置 logging.config=classpath:logback.xml 出现找不到这个文件的错误 经发现是maven的一个写法问题,本来我是打 ...
- 多个tomcat配置
在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...
- 三种角度解释href/src/link/import区别
网上查到的几种不同但比较容易理解的解释 解释一: href是Hypertext Reference的缩写,表示超文本引用.用来建立当前元素和文档之间的链接.常用的有:link.a.例如: <li ...
- javascript图片预加载
图片预加载是非常常见的一个功能,PC和移动端都会用到,尤其是在移动端,只要涉及到较多图片的加载都会用到该技术.下面是移动端用到的,引入了zepto. <!DOCTYPE html> < ...
- 关于纯css写三角形在firefox下的锯齿问题
相信很多人都用过利用border来实现小三角箭头,百度一下,这类的文章多如牛毛,这里我还是啰嗦点把常用的方法陈列出来: .triangle_border_up{ width:; height:; bo ...
- 2d动画开发之PIXI开发
简单的移动小游戏只要引入pixi.min.js就可以, 如果要用spine动画(龙骨也支持导出spine格式的)就要引入pixi-spine.js 如果还有声音的支持引入pixi-sound.js 学 ...
- C语言--清理getchar缓存
getchar()采用了缓冲区,而getch()才是立即获取,所以要想再用getchar()获取正确的值必须先清空缓冲区,如果是windows操作系统,用fflush(stdin)函数或rewind( ...
- <Android Framework 之路>BootAnimation(1)
介绍 开机动画,BootAnimation,就是Android手机开机郭晨各种以一个展示给用户的界面,实际是一个多个帧组成的动画,在界面上进行一帧一帧的播放,形成开机动画的效果. 本文针对Androi ...
- Android Studio图形基础(AS开发实战第二章学习笔记)
图形基础 一.drawable 在代码中引用drawable文件可分为两种情况 (1)使用setBackgroundResource和setImageResource方法,可直接在参数中指定drawa ...