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

后端 未结 1 2027
离开以前
离开以前 2020-12-29 15:25

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.

1条回答
  •  心在旅途
    2020-12-29 15:43

    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
    }
    

    0 讨论(0)
提交回复
热议问题