以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

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(12-8)译 -&gt; 重新获取一个属性的原始值
  2. iOS APP提交上架最新流程(转)
  3. 真机远程调试 ( IOS Android 以及微信,weex)
  4. NPOI
  5. 排球比赛计分规则(P205页)
  6. Hibernate的关系配置
  7. Json-lib使用 转载
  8. 有关require package的应用
  9. Shell脚本中单引号(‘)和双引号(“)的使用区别[转载]
  10. BP神经网络的基本原理
  11. UVA 408 (13.07.28)
  12. postgres 错误duplicate key value violates unique constraint 解决方案
  13. Angular5的new feature
  14. Nginx篇--Nginx源码搭建
  15. Ubuntu16---安装mysql5.7未提示输入密码,安装后修改mysql密码默认密码
  16. wb 黑名单批量操作
  17. jQuery Distpicker插件 省市区三级联动 动态赋值修改地址
  18. 使用gradle多渠道打包
  19. 静态时序分析基础STA
  20. import tensorflow 报错,CentOS 升级 glibc

热门文章

  1. WEB项目构建优化之自动清除CSS中的图片缓存
  2. Golang教程:结构体
  3. 【Bigdecimal】
  4. [转]Install ASP.NET MVC 4 for Visual Studio 2010
  5. Make sure that the controller has a parameterless public constructor.
  6. mongodb-mms安装
  7. zookeeper【2】集群管理
  8. 2048小游戏4X4C语言
  9. 数据库字段值为null利用setInc方法无法直接写入
  10. 用一个小例子来谈谈javascript的运行机制