yii之relations关联非主键
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,不去关联主键,而且关联其中一个字段的值,怎么搞都搞不定,论坛也翻了个遍,不得不说,yii的论坛搜索功能真的很烂,每次用都要从一堆的内容里过滤信息,还不见的能找到自己想要的,而且手册也比较简单,对这些东西没有做比较深入的解答。
后来无意中看到有个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'),
);
再来个例子:http://www.jinyuanbao.cn/
有两张表,设计结构如下:
商品表:字段:p_id,member_id
商铺表:字段:shop_id,member_id
其中p_id,shop_id都是主键,member_id是索引
考虑过这个原因,是最初设计的时候,每一个普通用户都可以发布商品,所以商品表没有记录shop_id。
可能也会考虑过,每一个用户会有多个商铺吧,所以shop表的member_id也不是唯一索引。
然后,我用Yii在做表关联,事实上,在我们改动程序的时候,我们已经把普通用户能够发布商品这个功能去掉了,因此,事实上对我们来说member_id,其实肯定是于shop表中的member_id有对应关系。
于是我在ShopProduct的relations这样写
PHP代码
/**
* @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(
'member' => array(self::BELONGS_TO,'ShopMember','member_id','with'=>'extends','condition'=>"member.member_state = '1'"),
'shop' => array(self::BELONGS_TO , 'ShopInfo' , 'member_id','on'=>'t.member_id = shop.member_id' ),
);
}
是的,看上去好象没问题,但是实际却关联不上。
在Yii的代码中,关于relation的on是这样写的:
XML/HTML代码
'on': the ON clause. The condition specified here will be appended to the joining condition using the AND operator. This option has been available since version 1.0.2.
所以,上述的代码在SQL中显示出来就是 select * from product left outer join shop on (product.member_id = shop.shop_id and product.member_id = shop.member_id)//这个代码是伪代码,只是为了说明问题。
出现这种情况可以将foreignKey字段设为空,然后再指定ON。
PHP代码
/**
* @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(
'member' => array(self::BELONGS_TO,'ShopMember','member_id','with'=>'extends','condition'=>"member.member_state = '1'"),
'shop' => array(self::BELONGS_TO , 'ShopInfo' , '','on'=>'t.member_id = shop.member_id' ),
);
}
查找的时候必须使用'with'关键字,例如
Member::model()->with('shop')->findAll($criteria);
yii之relations关联非主键的更多相关文章
- ThinkPHP关联模型如何关联非主键
		
ThinkPHP关联模型默认是主键外键关联 官方并没有提供相关文档 如何实现非主键与非主键间之间的关联 <?php namespace Admin\Model; use Think\Model\ ...
 - hibernate关联非主键注解配置
		
现在有两张表:一张t_s_user用户表和t_s_user_serial_number用户序号表 CREATE TABLE `t_s_user` ( `id` ) NOT NULL, `email` ...
 - Jpa/Hibernate ManyToOne 关联非主键列 延迟加载失效
		
@ManyToOne配置延迟加载,如果是关联主键列, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "billid", ...
 - 关于hibernate非主键多对一关联
		
一直以来,用hibernate做多对一时,关联的都是主键,今天,同事设计的表,关联的却不是主键,这下麻烦了,hibernate的many-to-one默认关联的都是主键,原来,hibernate提供了 ...
 - Hibernate非主键关联
		
一. 非主键关联,我们进行外键关联时,通常使用的是主键,但有时候需要使用到其他列时可以通过以下方法设置: 注解中:@JoinColumn(name="city", referenc ...
 - MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析
		
文章出处:http://inter12.iteye.com/blog/1430144 MYSQL的全表扫描,主键索引(聚集索引.第一索引),非主键索引(非聚集索引.第二索引),覆盖索引四种不同查询的分 ...
 - 【mysql优化】mysql count(*)、count(1)、count(主键字段)、count(非主键字段)哪个性能最佳
		
测试结果为:count(*)和count(1)基本相等,count(非主键字段)最耗性能 -- 数据量 708254select count(*) from tmp_test1;-- avg 0.22 ...
 - mysql死锁-非主键索引更新引起的死锁
		
背景:最近线上经常抛出mysql的一个Deadlock,细细查来,长了知识! 分析:错误日志如下: 21:02:02.563 ERROR dao.CommonDao [pool-15-t ...
 - cassandra——可以预料的查询,如果你的查询条件有一个是根据索引查询,那其它非索引非主键字段,可以通过加一个ALLOW FILTERING来过滤实现
		
cassandra的索引查询和排序 转自:http://zhaoyanblog.com/archives/499.html cassandra的索引查询和排序 cassandra的查询虽然很弱,但 ...
 
随机推荐
- 通过JS,用a标签代替form中的submit
			
---恢复内容开始--- 有时候在使用表单的时候,不一定会用到表单中的input_submit来提交表单数据,可能会用a.button等来代替 然后自然而然地想到了用JS中的提交表单数据的动作 < ...
 - 2019牛客暑期多校训练营(第一场) - H - XOR - 线性基
			
https://ac.nowcoder.com/acm/contest/881/H 题意: 给定n个整数,求其中异或和为 \(0\) 的子集的大小的和. 题解思路: 首先转化为每个可以通过异或表示 \ ...
 - python学习第十九天三元运算符与php语言区别
			
三元运算符是条件语句的简写,常见的条件语句写三行,三元运算符只需要写一行,python三元运算符是怎么写的呢 1,常见条件判断 if a<b: print(a) else: print(b) 2 ...
 - G	a+b+c+d=?
			
G a+b+c+d=? 链接:https://ac.nowcoder.com/acm/contest/338/G来源:牛客网 题目描述 This is a very simple problem! Y ...
 - 搞定Oracle SCN -system change number
			
SCN是Oracle的内部时钟,用来反映数据库中所有变化,在运行过程中不断更新.SCN种类包括: (1)系统当前SCN (2)Checkpoint SCN ...
 - NHibernet 事务 修改操作,事务没提交,数据库数据却同步(修改)了
			
Nhibernet 缓存 由于查询出来的数据和缓存关联,更新之后就算事务没执行提交操作,数据库依旧会更新,解决方法, 清空缓存,实例不和缓存关联,如下面标红代码 public bool UpdateT ...
 - PyInstaller库的使用
			
PyInstaller库的使用 PyInstaller库用于将已经写好的py程序,转换成可以跨平台的可执行文件 使用方式 发布主要借助cmd命令行来实现.在当前目录的powershell下,输入 py ...
 - 自己关于SSM框架的搭建
			
第一步 导入相应的包 spring springmvc 需要的包 spring-webmvc spring-aop spring-beans apring-context spring-core sp ...
 - ArrayList、LinkedList、Vector区别
			
ArrayList.LinkedList.Vector均为可伸缩数组,即可以动态改变长度的数组. 比较ArrayList和Vector: 1. 共同点: ArrayList和Vector都是基于Obj ...
 - Java使用对象类型作为方法的返回值