I have a chat table with fields
admin TINYINT
owner_id INTEGER
The goal is to have two relations in Yii:
'admin'=>array(
self::BELONGS_TO, 'Admin', 'owner_id',
'condition'=>'admin=1',
),
'user'=>array(
self::BELONGS_TO, 'User', 'owner_id',
'condition'=>'admin=0',
),
However, I got General error: 1 no such column: admin, and could manage only by adding all_ones and all_zeros columns to Admin table, so I could write
'admin'=>array(
self::BELONGS_TO, 'Admin', array('owner_id' => 'id', 'admin' => 'all_ones'),
),
'user'=>array(
self::BELONGS_TO, 'User', array('owner_id' => 'id', 'admin' => 'all_zeros'),
),
What is the way I could implement that without using such a hacks?
Solution 1:
Put your relations on User model (because the relation condition does filter the related model, not the model the relation is defined on)
Solution 2:
Use finder to get a filtered set of Chats (or DataProvider)