是否可以在Yii 1.1.14中使用一个“object_rel”表连接这些表?
object_rel:
location(type = 2):
程序(type = 1):
category(type = 3):
或者我必须写一些定制的花哨的东西?
我尝试过没有运气的MANY_TO_MANY ......
答案 0 :(得分:0)
不幸的是,我不认为关系是可行的,因为它们要求你指定模型类。但是,您可以在object_rel的模型中定义类似的内容:
private static $modelTypes = array(
1 => Program::model(),
2 => Location::model(),
3 => Category::model(),
);
public static function getOwner($object)
{
$data = self::$db->createCommand()
->select('owner_id, owner_type')
->from('object_rel')
->where('owner_id = :object_id', array(':object_id' => $object->id))
->queryRow(true); // assuming every object only has one owner
// no results found
if ($data === false) {
return false;
}
$model = self::$modelTypes[data['owner_type']];
return $model->findByPk(data['owner_id']);
}