yii多列外键的关系 [英] yii relation for multiple column foreign keys
本文介绍了yii多列外键的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表朋友(PersonA,PersonB)
。这些是 Person(id,name)
I have a table Friend (PersonA, PersonB)
. These are foreign keys of Person(id, name)
.
我想在它们之间创建一个Yii关系。这是我想出的:
I want to create a Yii relation between them. This is what I have come up with:
public function relations() {
return array(
'friends1' => array(self::HAS_MANY, 'Friend', 'PersonA'),
'friends2' => array(self::HAS_MANY, 'Friend', 'PersonB'),
);
}
有没有办法将这两个关系合并成一个?我希望这样的:
Is there a way to combine these two relations into one? I was hoping for something like this:
public function relations() {
return array(
'allFriends' => array(self::HAS_MANY, 'Friend', 'PersonA, PersonB'),
);
}
有任何想法吗?
EDIT#1:
为了完整起见,我们还假设我想订购 friends1
和 friends2
如下:
For completeness, let's also imagine that I want to order friends1
and friends2
like this:
public function relations() {
return array(
'friends1' => array(self::HAS_MANY, 'Friend', 'PersonA', 'order'=>'id ASC'),
'friends2' => array(self::HAS_MANY, 'Friend', 'PersonB', 'order'=>'id ASC'),
);
}
推荐答案
覆盖模型中的 __ get
函数:
public function __get($name)
{
if(($name == 'friends1') || ($name == 'friends2')) {
return parent::__get('friends1') + parent::__get('friends2');
}
else
return parent::__get($name);
}
这篇关于yii多列外键的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文