Referencing table with two different foreign keys to the same table in Kohana ORM
-
20-09-2019 - |
Question
table user:
|id|name|employee_priority_id|user_priority_id|
table priority:
|id|name|
As you can see, there are two foreign fields to the same table. But Kohana ORM default looks for a field called priority_id, which doesn't exist.
Is there a way to let Kohana ORM know that those two fields are an foreign key to that table.
Solution
You can use 'aliasing' as documented @ http://docs.kohanaphp.com/libraries/orm/advanced#aliasingenhancing_the_meaning_of_your_relationships
So in your case, your User_Model would be:
class User_Model extends ORM {
protected $belongs_to = array('employee_priority' => 'priority', 'user_priority' => 'priority');
}
BTW, according to Kohana's convention table names ought to be in plural form unless you override the $table_name, e.g:
class Priority_Model extends ORM {
protected $table_name = 'priority';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow