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关联非主键的更多相关文章

  1. ThinkPHP关联模型如何关联非主键

    ThinkPHP关联模型默认是主键外键关联 官方并没有提供相关文档 如何实现非主键与非主键间之间的关联 <?php namespace Admin\Model; use Think\Model\ ...

  2. hibernate关联非主键注解配置

    现在有两张表:一张t_s_user用户表和t_s_user_serial_number用户序号表 CREATE TABLE `t_s_user` ( `id` ) NOT NULL, `email` ...

  3. Jpa/Hibernate ManyToOne 关联非主键列 延迟加载失效

    @ManyToOne配置延迟加载,如果是关联主键列, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "billid", ...

  4. 关于hibernate非主键多对一关联

    一直以来,用hibernate做多对一时,关联的都是主键,今天,同事设计的表,关联的却不是主键,这下麻烦了,hibernate的many-to-one默认关联的都是主键,原来,hibernate提供了 ...

  5. Hibernate非主键关联

    一. 非主键关联,我们进行外键关联时,通常使用的是主键,但有时候需要使用到其他列时可以通过以下方法设置: 注解中:@JoinColumn(name="city", referenc ...

  6. MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析

    文章出处:http://inter12.iteye.com/blog/1430144 MYSQL的全表扫描,主键索引(聚集索引.第一索引),非主键索引(非聚集索引.第二索引),覆盖索引四种不同查询的分 ...

  7. 【mysql优化】mysql count(*)、count(1)、count(主键字段)、count(非主键字段)哪个性能最佳

    测试结果为:count(*)和count(1)基本相等,count(非主键字段)最耗性能 -- 数据量 708254select count(*) from tmp_test1;-- avg 0.22 ...

  8. mysql死锁-非主键索引更新引起的死锁

    背景:最近线上经常抛出mysql的一个Deadlock,细细查来,长了知识! 分析:错误日志如下: 21:02:02.563 ERROR dao.CommonDao        [pool-15-t ...

  9. cassandra——可以预料的查询,如果你的查询条件有一个是根据索引查询,那其它非索引非主键字段,可以通过加一个ALLOW FILTERING来过滤实现

    cassandra的索引查询和排序 转自:http://zhaoyanblog.com/archives/499.html   cassandra的索引查询和排序 cassandra的查询虽然很弱,但 ...

随机推荐

  1. 微信小程序(一)--微信小程序的介绍

    一.微信小程序简介 小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用.也体现了“用完即走”的理念,用户不用关心是否安装太多应用的问题.应用将无 ...

  2. Linux固定ip配置

    第一步:查看网络信息 [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu ...

  3. Robot Framework 常见问题处理方式

    1.运行testcase报错error 2解决办法 打开python的scripts目录,看看是否有pybot.bat文件,没有则创建一个 pybot.bat内容: @Echo off python ...

  4. JavaScript常用字符串方法和属性

    一直以来  在喜马拉雅上听  陪你读书(JavaScript WEB前端)  主播沙翼 讲的很好  果断买了这本书  现在做个笔记 var str = ‘abcd’ str.charAt(0); // ...

  5. MS DOS 常用命令整理

    最近在开发用到一些dos下的一些指令,还有bat文件,特别是bat的便捷性让我在生活和开发过程中好好使用. dos指令: java com.pdcss.util.JacobService > D ...

  6. JS高级 — 函数中的this指向问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. python常用函数 M

    max(iterable) 求最大值,可以传入key. 例子: min(iterable) 求最小值,支持传入key. 例子: match(regular expression, string) 字符 ...

  8. JDBC和驱动类Driver

    什么是JDBC? JDBC(Java DataBase Connectivity),是一套面向对象的应用程序接口(API),制定了统一的访问各类关系数据库的标准接口,为各个数据库厂商提供了标准的实现. ...

  9. Spring---Spring Aware

    1.概述 1.1.Spring中的  所有Bean  对Spring容器的存在  是没有意识的(即你可以将容器换成别的容器,这样使用容器与Bean之间的耦合度很低): 但在实际项目中,不可避免的要用到 ...

  10. 为什么js的"关联数组"不能转成json字符串而对象可以?

    定义这么一个js的“关联数组”: var arr = new Array(); arr[; arr[; alert(JSON.stringify(arr)); 得到的结果如图: [] 一句话,你的 a ...