Yii中的relations方法
以Blog示例: 重点看注释
User类中的relations方法如下
- <span style="font-size:18px;background-color: rgb(255, 255, 255);"> 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,
- ),
- );
- }</span>
Post中的方法如下 :
- <span style="font-size:18px;background-color: rgb(255, 255, 255);">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
- ),
- );
- }
- </span>
Comment中的ralations方法如下:
- <span style="font-size:18px;background-color: rgb(255, 255, 255);">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
- );
- }
- </span>
在控制器中写个方法测试一下结果:
- <span style="font-size:18px;background-color: rgb(255, 255, 255);"> 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>";
- }
- }</span>
- 原文来自 http://blog.csdn.net/littlebearwmx/article/details/8561018
Yii中的relations方法的更多相关文章
- C#中DataTable中的Compute方法使用收集
原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...
- Yii中的错误及异常处理
Yii中的错误及异常处理 Yii已经默认已经在CApplication上实现了异常和错误的接管,这是通过php的set_exception_handler, set_error_handler实现的. ...
- Yii中事件触发机制
控制器初始化中添加事件处理方法,在需要触发的地方直接触发 public function init() { parent::init(); // TODO: Change the autogenera ...
- 在yii中使用分页
yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...
- yii中的自定义组件
yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...
- YII中的表单挂件
利用助手(widget)在页面实现表单 控制器中 <?php class YiiFormController extends Controller { public function actio ...
- Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)
英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...
- yii 中设置提示成功信息,错误提示信息,警告信息
方法一: <?php Yii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlas ...
- 解密yii中CModule::_components和CModule::_componentConfig
array CModule::_components 所有组件对象(CComponent的子类)将作为键值存在该数组中, 键名是定义该组件时使用的键名.例如: protected function r ...
随机推荐
- Codeforces 981F. Round Marriage
Description 一个长度为 \(L\) 的环上有 \(n\) 个黑点和 \(n\) 个白点 , 你需要把黑点和白点配对 , 使得配对点的最大距离最小 , 最小距离定义为两点在环上的两条路径的最 ...
- C#发送GET与POST请求
////////HTTPGET HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = ...
- php数组操作,js数组操作
俩语言一块儿学老混.整理一下. php: 1.声明: 1)$arr1 = array('key1' => 'value1', 'key2' => 'value2' ...);//关联数组, ...
- jQuery插件 -- UI插件Tabs Widget 1.10
最近将tabs(标签页)整合到Admin LTE( 一个完全响应式管理模板,基于Bootstrap3的框架.)中使用.过程中走了不少弯路,原因是没有注意版本问题,jquery ui tabs 每次升级 ...
- ThreadLocal介绍以及源码分析
ThreadLocal 线程主变量 前面部分引用其他优秀博客,后面源码自己分析的,如有冒犯请私聊我. 用Java语言开发的同学对 ThreadLocal 应该都不会陌生,这个类的使用场景很多,特别是在 ...
- springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页、个人作品、个人博客)
前言 My Site 主要功能有:个人首页.个人作品.个人博客为一体的站点,网站的文章和作品均由markdown进行编写,可以满足你的基本需求.如果觉得这个项目不错,请为它点赞支持. 项目架构 JDK ...
- mysql Backup &recovery
备份数据库非常重要,这样您就可以恢复数据,并在发生问题时重新启动并运行,例如系统崩溃,硬件故障或用户错误地删除数据. 在升级MySQL安装之前,备份也是必不可少的保护措施,它们可用于将MySQL安装转 ...
- 程序员学炒股(6) 让我们来看一下7月份A股的表现
有了前几天的数据准备,那让我们看一下7月份的股市是牛市还是熊市呢? 牛市和熊市主要就看股票的涨跌分布情况,虽然我们看大盘数据,但是不过中石油占的A股市值太大了,并且中石油95%的股票都是大股东控制的相 ...
- Flex和MyEclipse10整合时候需要注意的问题
1.myeclipse和flex的位数要一致,不能混着安装 2.独立安装完Adobe Flash Builder 4.6 Installer之后,在其的安装文件夹下有一个utilities文件夹下有一 ...
- 项目经验:GIS<MapWinGIS>建模第三天
记录下GIS工程进展