Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

orm

kohana

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.

like image 979
dusan Avatar asked Dec 22 '09 13:12

dusan


People also ask

How to Add fields to a PivotTable?

On the Analyze tab, in the Calculations group, click Fields, Items, & Sets, and then click Calculated Field. In the Name box, type a name for the field. In the Formula box, enter the formula for the field. To use the data from another field in the formula, click the field in the Fields box, and then click Insert Field.

How to make columns in PivotTable?

Add an Additional Row or Column Field Click any cell in the PivotTable. The PivotTable Fields pane appears. You can also turn on the PivotTable Fields pane by clicking the Field List button on the Analyze tab. Click and drag a field to the Rows or Columns area.

How to manually Move columns in a PivotTable?

Change the order of row or column items In the PivotTable, right-click the row or column label or the item in a label, point to Move, and then use one of the commands on the Move menu to move the item to another location.

How to view PivotTable fields?

The Field List should appear when you click anywhere in the PivotTable. If you click inside the PivotTable but don't see the Field List, open it by clicking anywhere in the PivotTable. Then, show the PivotTable Tools on the ribbon and click Analyze> Field List.


1 Answers

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
}
like image 103
Lukman Avatar answered Sep 20 '22 15:09

Lukman