在Kohana ORM中将具有两个不同外键的表引用到同一个表中

时间:2009-08-28 12:48:34

标签: orm kohana database-relations

table user:
|id|name|employee_priority_id|user_priority_id|
table priority:
|id|name|

如您所见,同一个表中有两个外来字段。但Kohana ORM默认查找名为priority_id的字段,该字段不存在。

有没有办法让Kohana ORM知道这两个字段是该表的外键。

1 个答案:

答案 0 :(得分:2)

您可以使用@ http://docs.kohanaphp.com/libraries/orm/advanced#aliasingenhancing_the_meaning_of_your_relationships

记录的'别名'

因此,在您的情况下,您的User_Model将是:

class User_Model extends ORM {
    protected $belongs_to = array('employee_priority' => 'priority', 'user_priority' => 'priority');
}
根据Kohana的惯例,BTW应该是复数形式,除非你覆盖$ table_name,例如:

class Priority_Model extends ORM {
    protected $table_name = 'priority';
}