yii框架详解 之 CActiveRecord
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * @return array relational rules. */ 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( 'visas'=>array(self::MANY_MANY, 'Visa', '{{visa_to_order}}(id_order, id_visa)', 'select'=>array( '*', #获取所有字段 'visas_visas.price AS price_visa', #visas_visas是关系表的别名,获取关系表里的字段值 ), ), 'user'=>array(self::BELONGS_TO, 'User','id_customer)'), ); } |
Yii的relations里self::BELONGS_TO默认是用当前指定的键跟关联表的主键进行join,例如:
return array(
'reply' => array(self::BELONGS_TO, 'BookPostReply', 'postid'),
);默认生成的sql是 on id = postid,id是BookPostReply的主键。
但今天我遇到的需求却是需要生成 on BookPostReply.postid = t.postid,不去关联主键,
后来无意中看到有个on的属性,刚好跟sql里的on一样,于是抱着试试的想法,在配置里加了上去
return array(
'reply' => array(self::BELONGS_TO, 'BookPostReply', 'postid', 'on' => 't.postid=reply.postid'),
);看调试信息里的SQL语句,发现yii生成了一条 on id = postid and t.postid=reply.postid 这样的语句,看到这就已经明白这个东西的作用了,于是将postid清空,改成如下:
return array(
'reply' => array(self::BELONGS_TO, 'BookPostReply', '', 'on' => 't.postid=reply.postid'),
);其实,Yii的文档有说明: 如果你需要指定自定义的pk->fk关联,你可以定义数组(‘fk’=> ‘pk')。
【多条件下关联查询结果集】
案例:
$this->news = News::model()->with(array(
'sites'=>array('condition'=>'sites.id_site='.Yii::app()->params['siteId'],'together'=>true),
'categories'=>array('condition'=>'categories.id_category='.$this->categoryId,'together'=>true),
))->findAll(array(
'limit'=>$this->limit,
'condition'=>'t.active=1',
'order'=>'t.position'
));
使用with('关系名称1','关系名称2')方法,将延后查询关联表,
若如上案例,在with方法中,返回关系数组,将更灵活。
若想即时联合查询,需要添加'together'=>true属性值,才会组合为一句SQL进行查询。
我们来看下CActiveRecord.php文件里,yii是如何设计with()方法的。
/**
* Specifies which related objects should be eagerly loaded.
* This method takes variable number of parameters. Each parameter specifies
* the name of a relation or child-relation. For example,
* <pre>
* // find all posts together with their author and comments
* Post::model()->with('author','comments')->findAll();
* // find all posts together with their author and the author's profile
* Post::model()->with('author','author.profile')->findAll();
* </pre>
* The relations should be declared in {@link relations()}.
*
* By default, the options specified in {@link relations()} will be used
* to do relational query. In order to customize the options on the fly,
* we should pass an array parameter to the with() method. The array keys
* are relation names, and the array values are the corresponding query options.
* For example,
* <pre>
* Post::model()->with(array(
* 'author'=>array('select'=>'id, name'),
* 'comments'=>array('condition'=>'approved=1', 'order'=>'create_time'),
* ))->findAll();
* </pre>
*
* @return CActiveRecord the AR object itself.
*/
public function with()
{
if(func_num_args()>0)
{
$with=func_get_args();
if(is_array($with[0])) // the parameter is given as an array
$with=$with[0];
if(!empty($with))
$this->getDbCriteria()->mergeWith(array('with'=>$with));
}
return $this;
}
我们再去看 CDbCriteria 类中的 mergeWith 方法定义:
/**
* Merges with another criteria.
* In general, the merging makes the resulting criteria more restrictive.
* For example, if both criterias have conditions, they will be 'AND' together.
* Also, the criteria passed as the parameter takes precedence in case
* two options cannot be merged (e.g. LIMIT, OFFSET).
* @param mixed $criteria the criteria to be merged with. Either an array or CDbCriteria.
* @param string|boolean $operator the operator used to concatenate where and having conditions. Defaults to 'AND'.
* For backwards compatibility a boolean value can be passed:
* - 'false' for 'OR'
* - 'true' for 'AND'
*/
public function mergeWith($criteria,$operator='AND'){......}
注意,这里就是我要强调的地方,yii默认且只执行关联表之间的AND条件查询,也就是说,在给定不同条件查询时,yii只为我们求取不同条件的交集结果返回。
但是在底层的CDbCriteria 类中,预留了参数$operator给我们。有需求的同学需要重写CActiveRecord来实现拓展了哦。
yii框架详解 之 CActiveRecord的更多相关文章
- yii框架详解 之 CWebApplication 运行流程分析
在 程序入口处,index.php 用一句 Yii::createWebApplication($config)->run(); 开始了app的运行. 那么,首先查看 CWebApplicat ...
- yii框架详解 之 国际化 (I18N)
我们要开启组件中们关于语言的配置,默认的就是CPhpMessageSource,也可以改为其他的方式. #组件配置中 'messages' => array( 'class'=> ...
- jQuery Validate验证框架详解
转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...
- mina框架详解
转:http://blog.csdn.net/w13770269691/article/details/8614584 mina框架详解 分类: web2013-02-26 17:13 12651人 ...
- lombok+slf4j+logback SLF4J和Logback日志框架详解
maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...
- [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解
Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...
- iOS 开发之照片框架详解(2)
一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...
- Quartz.NET作业调度框架详解
Quartz.NET作业调度框架详解 http://www.cnblogs.com/lmule/archive/2010/08/28/1811042.html
- mapreduce框架详解
hadoop 学习笔记:mapreduce框架详解 开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感 ...
随机推荐
- mapreduce 自定义数据类型的简单的应用
本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...
- UTF-8-BOM
https://www.zhihu.com/question/20167122 知乎 「带 BOM 的 UTF-8」和「无 BOM 的 UTF-8」有什么区别?网页代码一般使用哪个? http://b ...
- Javascript设计模式详解
Javascript常用的设计模式详解 阅读目录 一:理解工厂模式 二:理解单体模式 三:理解模块模式 四:理解代理模式 五:理解职责链模式 六:命令模式的理解: 七:模板方法模式 八:理解javas ...
- Shader 之 顶点变形
可以使3D物体通过顶点变形弯曲,常见于跑酷游戏的跑道.可向左.右.上.下弯曲. Shader "Custom/VertexColorCurved" { Properties { / ...
- linux下的库冲突问题
lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...
- PHP函数preg_replace() 正则替换所有符合条件的字符串
PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...
- 《深入浅出WPF》笔记三
1.Field:字段,封装在类中的变量. Method:方法,封装在类中的函数. 成员:类中的字段和方法,可分为静态成员和非静态成员. 静态字段在内存中只有一份拷贝. 非静态字段是每个实例拥有一个拷贝 ...
- windows 下wamp环境1 配置之apache的安装
一.安装apache2.4 打开网站 apachelounge.com https://www.apachelounge.com/ 点击左侧Downloads,然后选择对应的版本,这里选择Apa ...
- SQL Server中TEXT类型字段值在数据库中追加字符串方法
在数据上我们往往会遇到ntext大文本类型,这种类型如果和 nvarchar类型相加会出现问题,所以有一中方法可以解决这种问题. 使用的sql 函数: TEXTPTR:返回要更新的 text.nt ...
- 在framework中打包xib
废话不多说,直接上图 1.Copy Bundle Resources 中加入相关xib 2.这里是重点,调用的时候不能直接写 [[NSBundle mainBundle] loadNibNamed:@ ...