Yii中的relations方法

以Blog示例:重点看注释

User类中的relations方法如下

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,
    	),
    );
}

Post中的方法如下 :

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
		),
	); 
}

Comment中的ralations方法如下:

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
	);
}

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

	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>";
	}
}

留下评论

鄂ICP备13000209号-1

鄂公网安备 42050602000277号