Thinkphph 使用RelationModel的三表关联查询机制

有如下三个表

a表 b表 c表
id bid other id cid other id other

a表的bid关联b表的id,b表的cid关联c表的id

现在需要查询a表的时候顺带把b表和c表的相关信息也查询出来

 

很多人一开始就想到使用原生的sql语句
select * from a left join b on a.bid=b.id left join c on b.cid=c.id;
但这并不是我想要的!

 

直接给出使用案例

class AModel extends RelationModel
{
    protected $_link = array(
        'B'=>array(
        'class_name' => 'B',
        'mapping_type' => self::BELONGS_TO,
        'mapping_name' => 'b',
        'foreign_key' => 'bid',//关联id
    ),
    //c表要间接关联b表
    'C' => array(
        'class_name' => 'C',
        'mapping_type' => self::MANY_TO_MANY,
        'mapping_name' => 'c',
        'mapping_key' => 'bid', // a.bid, a表要关联b表的字段
        'foreign_key' => 'id', // c.id 
        'relation_foreign_key' => 'cid', // b.cid b表要关联c表的字段
        'relation_table' => '__B__', //b表,双下划线加大写,会自动转换统一格式的表,如果多个大写例如BFModel,写成__B_F__中间需要用一个下划线隔开
    ),
);
    

  

转载于:https://www.cnblogs.com/chriiess/p/8652540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值