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

醉酒当歌 提交于 2019-11-29 03:21:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!