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'),
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')),
);
try
public function relations() {
return array(
'shop' => array(self::HAS_ONE, 'Shop', 'target_id', array(
'condition' => 'ledger.target_type = :type',
'params' => array(':type'=>'shop')
)),
);
}