zoukankan      html  css  js  c++  java
  • 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);
    

      

    个人作品展示:www.jinyuanbao.cn
  • 相关阅读:
    需要了解的项目
    vmware安装问题:Microsoft Runtime DLL安装程序未能完成安装
    rabbitmqctl 报错
    RabbitMQ快速入门python教程
    RabbitMQ消息队基本概念
    Windows RabbitMQ 添加用户、设置角色和权限 (包含无法添加的错误处理)
    转:Window10下RabbitMQ安装图文教程
    Windows下RabbitMQ安装及入门
    crontab 详细用法 定时任务
    转 RabbitMQ
  • 原文地址:https://www.cnblogs.com/jyb2014/p/3912438.html
Copyright © 2011-2022 走看看