duannue2455 2016-10-27 01:39
浏览 60
已采纳

Yii与当前模型的条件关系

My question is how to build this relation in Yii model:

The table ledger:

|id|target_type|target_id|amount|

sample data would be like:

|1|shop|20|100.00|
|2|member|30|99.50|

as you can see, I am using the target_type as an indicator of which table to link to, while target_id is the id of the linked foreign table.

I tried this but not working

'shop' => array(self::HAS_ONE, 'shop', 'target_id', 'condition'=>'target_type=shop'),
  • 写回答

2条回答 默认 最新

  • douhuireng4407 2017-02-18 18:07
    关注

    I ended up with workaround like:

    return array(
                'shop' => array(self::BELONGS_TO, 'Shop', 'target_id'),
                'member' => array(self::BELONGS_TO, 'Member', array('target_id'=>'user_id')),
            );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部