Yii中的relations方法

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