Yii AR中处理多表关联的relations配置
关系型 Active Record官方文档中指出:
两张表之间的关联是根据外键来的,但是这种外键关联虽然在数据容错方面有益处,但是在性能上是个损伤,所以,一般是不定义外键的.
这种情况下,他们之间的关联又会根据什么来呢?
有A,B两张表,在A与B表中都存在一个字段filedX,并且同时字段filedX也是A,B的关联字段,这种情况下,可以直接
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(
'VarName'=>array('RelationType', 'ClassName', 'filedX', ...additional options)
);
}
这样运用relations方法.
另外有一种情况是,虽然A,B两张表,有关联,但是关联字段不是同一个名字,如在A表中,关联B表的字段名是afieldb ,而在B表被A表关联的字段名是bfielda,则不能直接应用以上方式来进行联接查询,可以变通一下成这样:
return array(
'VarName'=>array('RelationType', 'ClassName', '', 'on' => ' t.afieldb = VarName.bfielda ',...additional options)
);
解释一下:
array的前三个元素是不能变了,他们的意义与他们的顺序相对应,第一个元素一定得是关联类型,第二个元素得是被关联表model的类名,第三个元素得
是关联字段,除些三个,其它元素则与位置关系不大了,不过,必须指定元素的key,如本例中的'on'就是键名,这里的第三个元素是空,就两个单引号,后
面'on'的数据键值指定他们的关联关系,t.afieldb,表示主表字段afieldb,VarName.bfielda表示被关联表字段
bfielda,顺便说明一下,在所有relations关系中,主表都有个别名,并且这个别名就是t,而被关联表也有一个别名,这个别名就是定义的
VarName,如:
'shops'=>array(self::HAS_ONE, 'SubjectShops', '' , 'on'=>' t.sid = shops.id '),
其中,被关联表别名就是shops。
除了这个'on'元素外,在关系声明时可以指定附加的选项,概括如下:
select: 关联的 AR 类中要选择(select)的列的列表。 默认为 '*',即选择所有列。此选项中的列名应该是已经消除歧义的。
condition: 即 WHERE 条件。默认为空。此选项中的列名应该是已经消除歧义的。
params: 要绑定到所生成的 SQL 语句的参数。应该以 名-值 对数组的形式赋值。此选项从 1.0.3 版起有效。
on: 即 ON 语句。此处指定的条件将会通过 AND 操作符附加到 join 条件中。此选项中的列名应该是已经消除歧义的。 此选项不会应用到 MANY_MANY 关系中。此选项从 1.0.2 版起有效。
order: 即 ORDER BY 语句。默认为空。 此选项中的列名应该是已经消除歧义的。
with:
a list of child related objects that should be loaded together with
this object. Be aware that using this option inappropriately may form an
infinite relation loop.
joinType: type of join for this relationship. It defaults to LEFT OUTER JOIN.
alias:
the alias for the table associated with this relationship. This option
has been available since version 1.0.1. It defaults to null, meaning the
table alias is the same as the relation name.
together: whether
the table associated with this relationship should be forced to join
together with the primary table and other tables. This option is only
meaningful for HAS_MANY and MANY_MANY relations. If this option is set
false, the table associated with the HAS_MANY or MANY_MANY relation will
be joined with the primary table in a separate SQL query, which may
improve the overall query performance since less duplicated data is
returned. If this option is set true, the associated table will always
be joined with the primary table in a single SQL query, even if the
primary table is paginated. If this option is not set, the associated
table will be joined with the primary table in a single SQL query only
when the primary table is not paginated. For more details, see the
section "Relational Query Performance". This option has been available
since version 1.0.3.
join: the extra JOIN clause. It defaults to empty. This option has been available since version 1.1.3.
group: the GROUP BY clause. It defaults to empty. Column names referenced in this option should be disambiguated.
having:
the HAVING clause. It defaults to empty. Column names referenced in
this option should be disambiguated. Note: option has been available
since version 1.0.1.
index: the name of the column whose values
should be used as keys of the array that stores related objects. Without
setting this option, an related object array would use zero-based
integer index. This option can only be set for HAS_MANY and MANY_MANY
relations. This option has been available since version 1.0.7.
In addition, the following options are available for certain relationships during lazy loading:
limit: limit of the rows to be selected. This option does NOT apply to BELONGS_TO relation.
offset: offset of the rows to be selected. This option does NOT apply to BELONGS_TO relation.
Yii AR中处理多表关联的relations配置的更多相关文章
- 数据库MySQL中关于“多表关联更新”的那些事
在常见的sql中,我们经常在查询中进行多表关联查询,用的比较熟练.今天在开发中遇到一个实际业务场景是多表关联更新,一时不知所措.本着多学习的态度,没有直接写java代码去实现,终于把多表关联更新的sq ...
- 详解MongoDB中的多表关联查询($lookup)
一. 聚合框架 聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息. 聚合管道操作主要包含下面几个部分: 命令 功能描述 $projec ...
- 详解MongoDB中的多表关联查询($lookup) (转)
一. 聚合框架 聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息. 聚合管道操作主要包含下面几个部分: 命令 功能描述 $projec ...
- sql中修改多表关联的字段
表1:USERID USERNAME USERREMARK 表2:ROLEID USERID ROLENAME 其中表1的USERID与表2的USERID为关联字段. 若现在只知道ROLEID,要修改 ...
- Mysql中实现多表关联查询更新操作
今天一下要记录一下才行了,每次都要去网上查找方法,每次都难找得要命 Mysql在更新某些字段的数据时,有时候会依据其他表的数据进行更新,需要通过关联后对不同的行更新不同的值,传统的update set ...
- Yii框架中的form表单
<?php//引入命名空间use yii\helpers\Html;?><?php //表单:Html::beginForm(提交地址,提交方法,属性数组);?><?=H ...
- yii框架中应用jquery表单验证插件
效果图: 视图层: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- spring data jpa 表关联设置用户表关联角色表配置
User 表: @ManyToMany(cascade = { CascadeType.MERGE }) @JsonIgnore @JoinTable(name = "UserRole&qu ...
- Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询
在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...
随机推荐
- 【请您听我说】PHP语法特点的一些看法
一.基本认识 PHP是干什么的?百度百科上提到说:PHP就是一门脚本语言,开发用的,相信这个你们只要去搜一下,就会有一大堆关于PHP概念的解释. 相信我们对PHP的初步认识是从浏览器开始的吧,当我们每 ...
- c++(八皇后)
八皇后是一道很具典型性的题目.它的基本要求是这样的:在一个8*8的矩阵上面放置8个物体,一个矩阵点只允许放置一个物体,任意两个点不能在一行上,也不能在一列上,不能在一条左斜线上,当然也不能在一条右斜线 ...
- 客户端一致性与多Leader机制------《Designing Data-Intensive Applications》读书笔记7
接着上一篇的内容,我们继续来梳理分布式系统之中的副本机制与副本一致.上文我们聊到了在可用性与一致性之间的一个折中的一致性等级:最终一致性.我们顺着上篇的内容,由用户来分析一致性等级. 1. 客户端的困 ...
- DevGridControl中GridView排序问题
在对表格数据源为字符串类型的列排序时,为了实现按照值大小进行排序,需要进行以下处理: 先设置该列SortMode属性为自定义属性 gridColumn1.SortMode = DevExpress.X ...
- 十二个 ASP.NET Core 例子——1.1版本 EF MySql快速搭建
core1.0的时候搭建过一次mysql EF. 一大推问题.最近在core1.1 又重新搭了一次.简单搭建还挺快,没出现什么幺蛾子.总结下步骤 建立项目,例如ASP.NET Core1.1 WebA ...
- thinkphp5.0 文章详情页 上一篇 下一篇
// 上一篇下一篇(同一个分类下,先确定该分类的pid) public function frontAfter() { $param=$this->param; $front=Db::name( ...
- ADO.NET复习总结(5)--工具类SqlHelper 实现登录
工具类SqlHelper 即:完成常用数据库操作的代码封装 一.基础知识1.每次进行操作时,不变的代码: (1)连接字符串:(2)往集合存值:(3)创建连接对象.命令对象:(4)打开连接:(5)执行命 ...
- mybatis_helloworld(2)_源码
摘录自:http://blog.csdn.net/y172158950/article/details/16982123 在helloworld(1)中,分析了insert一条数据的流程,现在分析下源 ...
- JavaScript return 最简单解释
一.return 返回值 1)函数名字 +括号 :fun() ==> retrun 后面的值 2)所以函数的模范返回值是为未定义 3)return; 后面的任何代码都不会执行了 二.arguem ...
- 我看过得最易懂的一段AOP的解释
http://blog.csdn.net/zhangliangzi/article/details/51648032 面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ...