Kohana 3.0.x ORM: Read additional columns in pivot tables
Asked Answered
C

1

8

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.

Colic answered 22/12, 2009 at 13:25 Comment(0)
S
9

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
}
Starrstarred answered 23/12, 2009 at 1:45 Comment(3)
You're showing to retrieve a value from a pivot but can you also show a way to initially set the value in the pivot?Orling
where would you store that model? ...models/a/b.php ?Errhine
@Brenden: Yes, models/a/b.phpColic

© 2022 - 2024 — McMap. All rights reserved.