Kohana 3.0.x ORM:阅读数据透视表中的其他列 [英] Kohana 3.0.x ORM: Read additional columns in pivot tables
问题描述
我正在使用Kohana v3和ORM,我有两个模型,Model_A
和Model_B
通过枢轴表与"has_many"相关,该表有一个附加列.我可以使用add()
函数的第三个参数将数据保存在数据透视表的该列中,但是我无法弄清楚如何使用ORM读取该列.
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.
推荐答案
如果您要访问该附加列,则需要创建一个基于该数据透视表的模型,假设我们将其命名为Model_A_B
.>
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()
);
}
然后,如果$a
是Model_A
的实例,并且$b
是Model_B
的实例,我们可以通过调用以下方式获得Model_A_B
实例:
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
}
这篇关于Kohana 3.0.x ORM:阅读数据透视表中的其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!