问题
I'm using Kohana v3 and ORM, I have two models, Model_A
and Model_B
related by "has_many" through a pivot table, which has an additional column. I can save data in that column in the pivot table using the third parameter of the add()
function, but I can't figure out how to read that column using ORM.
Any ideas? Thanks in advance.
回答1:
You need to create a Model that is based on that pivot table if you want to access that additional column, let say we name it Model_A_B
.
class Model_A_B extends ORM {
protected $_belongs_to = array(
'A' => array(),
'B' => array()
);
}
Then, if $a
is an instance of Model_A
and $b
is an instance of Model_B
, we get the Model_A_B
instance by calling:
$ab = ORM::factory('A_B', array('A_id' => $a, 'B_id' => $b));
if ($ab->loaded()) {
// do stuff
}
来源:https://stackoverflow.com/questions/1946357/kohana-3-0-x-orm-read-additional-columns-in-pivot-tables