Yii中的relations方法

本文介绍了Yii框架中的relations方法,通过一个Blog示例展示了如何在User类中定义并使用relations,以及在控制器中进行测试的方法,帮助理解如何操作关联数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值