以Blog示例: 重点看注释

User类中的relations方法如下

  1. <span style="font-size:18px;background-color: rgb(255, 255, 255);"> public function relations()
  2. {
  3. return array(
  4. 'posts' => array(self::HAS_MANY, 'Post', 'author_id',
  5. 'order'=>'posts.update_time DESC',
  6. 'with'=>'comments:approved',  // $user = User::model()->findByPk(1); 这里也查出了每篇post所带的comments
  7. //approved是comment的命名空间,可以在这里设置
  8. //'together'=>false,  设置这一项,关联查新将被分为几个SQL语句执行,和性能有关系
  9. ),
  10. 'postCount'=>array(
  11. self::STAT,'Post','author_id',
  12. 'condition'=>'status='.Post::STATUS_PUBLISHED,
  13. ),
  14. );
  15. }</span>

Post中的方法如下 :

  1. <span style="font-size:18px;background-color: rgb(255, 255, 255);">public function relations()
  2. {
  3. // NOTE: you may need to adjust the relation name and the related
  4. // class name for the relations automatically generated below.
  5. return array(
  6. 'author'=>array(self::BELONGS_TO,'User','author_id',
  7. //'select'=>'id,username,profile',    // 关联查询的选项,如果不设置,则默认为*即整个关联记录
  8. ),
  9. 'comments'=>array(self::HAS_MANY,'Comment','post_id',
  10. 'condition'=>'comments.status='.Comment::STATUS_APPROVED,
  11. 'order'=>'comments.create_time DESC'),
  12. 'commentCount'=>array(
  13. self::STAT,'Comment','post_id',
  14. 'condition'=>'status='.Comment::STATUS_APPROVED
  15. ),
  16. );
  17. }
  18. </span>

Comment中的ralations方法如下:

  1. <span style="font-size:18px;background-color: rgb(255, 255, 255);">public function attributeLabels()    //名字和现实标签的映射数组
  2. {
  3. return array(
  4. 'id' => 'Id',
  5. 'content' => 'Comment',
  6. 'status' => 'Status',
  7. 'create_time' => 'Create Time',
  8. 'author' => 'Name',
  9. 'email' => 'Email',
  10. 'url' => 'Website',
  11. 'post_id' => 'PostID',   //对应的博客ID
  12. );
  13. }
  14. </span>

在控制器中写个方法测试一下结果:

  1. <span style="font-size:18px;background-color: rgb(255, 255, 255);"> public function actionRQ(){
  2. $post = Post::model()->find('id=:id',array(':id'=>7));
  3. echo $post->author->username;
  4. echo "<hr>";
  5. $posts = Post::model()->with('author','comments')->findAll();   //急切加载
  6. foreach($posts as $post){
  7. echo $post->id."  |";
  8. foreach($post->comments as $comment){
  9. echo $comment->id.": ";
  10. echo $comment->content."<br>";
  11. }
  12. echo $post->author->username."<br>";
  13. }
  14. echo "<hr>";
  15. $user = User::model()->with('posts.comments')->findByPk(1);
  16. //$user = User::model()->findByPk(1);  这一句和上一句是一样的,因为在User的relations声明的posts也已经加上了关联查询:with=>'comments'
  17. foreach($user->posts as $post){    //嵌套急切加载
  18. echo $post->title." (";
  19. foreach($post->comments as $comment){
  20. echo $comment->id." : ";
  21. echo $comment->content."<br>";
  22. }
  23. echo ")".$post->tags."<br>";
  24. }
  25. echo "<hr>";
  26. $criteria = new CDbCriteria;
  27. //$criteria->select = "username";
  28. //$criteria->order
  29. //$criteria->limit
  30. //$criteria->condition
  31. //$criteria->params
  32. $criteria->with = array(
  33. 'posts.comments',
  34. );
  35. $users = User::model()->findAll($criteria);
  36. foreach($users as $user){
  37. echo $user->username.":<br>";
  38. //echo $user->password;
  39. foreach($user->posts as $post){    //嵌套急切加载
  40. echo $post->title." (";
  41. foreach($post->comments as $comment){
  42. echo $comment->id." : ";
  43. echo $comment->content."<br>";
  44. }
  45. echo ")".$post->tags."<br>";
  46. }
  47. }
  48. //动态关联查询,也就是在查询的时候覆盖relations中设置的关联的选项
  49. echo "<hr>";
  50. $user=User::model()->with(array(
  51. 'posts'=>array(
  52. 'order'=>'posts.update_time DESC',
  53. 'with'=>array('comments'=>array('order'=>'comments.id ASC')),
  54. //'together'=>false,   //关联声明中设置together 选项为false 以便一些表被连接在单独的SQL语句中,分为几个SQL语句执行
  55. ),
  56. ))->findByPk(1);
  57. echo "demo 的posts数量为:".$user->postCount;
  58. echo "<br>";
  59. foreach($user->posts as $post){
  60. echo $post->id."(";
  61. echo $post->title."的评论数量是:".$post->commentCount."<br>";
  62. foreach($post->comments as $comment){
  63. echo $comment->id." | ";
  64. }
  65. echo ")";
  66. echo "<br>";
  67. }
  68. }</span>
  69. 原文来自 http://blog.csdn.net/littlebearwmx/article/details/8561018

Yii中的relations方法的更多相关文章

  1. C#中DataTable中的Compute方法使用收集

    原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...

  2. Yii中的错误及异常处理

    Yii中的错误及异常处理 Yii已经默认已经在CApplication上实现了异常和错误的接管,这是通过php的set_exception_handler, set_error_handler实现的. ...

  3. Yii中事件触发机制

    控制器初始化中添加事件处理方法,在需要触发的地方直接触发 public function init() { parent::init(); // TODO: Change the autogenera ...

  4. 在yii中使用分页

    yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...

  5. yii中的自定义组件

    yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...

  6. YII中的表单挂件

    利用助手(widget)在页面实现表单 控制器中 <?php class YiiFormController extends Controller { public function actio ...

  7. Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)

    英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...

  8. yii 中设置提示成功信息,错误提示信息,警告信息

    方法一: <?php Yii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlas ...

  9. 解密yii中CModule::_components和CModule::_componentConfig

    array CModule::_components 所有组件对象(CComponent的子类)将作为键值存在该数组中, 键名是定义该组件时使用的键名.例如: protected function r ...

随机推荐

  1. OAuth2.0 微信授权机制

    我在了解设计Restful接口的时候,发现涉及到接口验证,可以利用OAuth2.0机制来验证. 我开发的微信端Web网页通过微信授权的时候,微信端也是用OAuth2.0机制来获取用户基本信息. OAu ...

  2. css内容简介(层叠样式表)

    css是对网页编辑的加色,是对其功能的渲染. 根据规范每个元素都有一个display属性,每个元素都有一个------------如div元素他的默认为block. 行内元素和块级元素 块级元素会占据 ...

  3. 理解 Azure 虚拟机的性能监视

    随着越来越多的用户将生产应用迁移到云平台,一些传统 IT 的运维功能也相应的需要改变,例如监控,备份等等.我们希望通过这一系列的文章来协助用户更好的理解在 Azure 云平台上实现资源监控的方法. 在 ...

  4. Codis3.2 安装部署

    转载请注明出处:https://www.cnblogs.com/format-ch/p/9323841.html 一.软件下载 下载 下载 zookeeper (Codis注册中心) http://m ...

  5. Iphone各个型号机型的详细参数,尺寸和dpr以及像素

    1.iPhone尺寸规格 2.单位inch(英吋) 1 inch = 2.54cm = 25.4mm 3.iPhone手机宽高 上表中的宽高(width/height)为手机的物理尺寸,包括显示屏和边 ...

  6. side Effect

    副作用 side Effect 副作用是在计算结果的过程中,系统状态的一种变化,或者与外部世界进行的可观察的交互. 副作用可能包含,但不限于: 1.更改文件系统 2.往数据库里插入数据 3.发送一个h ...

  7. ES6中的import()函数

    import(specifier) 上面代码中,import函数的参数specifier,指定所要加载的模块的位置.import命令能够接受什么参数,import()函数就能接受什么参数,两者区别主要 ...

  8. 洛谷P1072 Hankson 的趣味题(数学)

    题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...

  9. ioc autofac简单示例

    1.winform用法: nuget安装autofac public interface ILog { bool Log(string msg); } public class TXTLogger : ...

  10. 1-2 Sass安装(windows版)

    在 Windows 平台下安装 Ruby 需要先有 Ruby 安装包,大家可以到 Ruby 的官网(http://rubyinstaller.org/downloads)下载对应需要的 Ruby 版本 ...