duannue2455 2016-10-27 09: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-19 02: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条)

报告相同问题?