Kohana 3.3 ORM _has_many _belongs_to [英] Kohana 3.3 ORM _has_many _belongs_to

查看:97
本文介绍了Kohana 3.3 ORM _has_many _belongs_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用内置的ORM在Kohana 3.3中设置产品对象.我想要这样,以便在我打电话时:

I am trying to set up a product object in Kohana 3.3 using the built in ORM. I want it so that when I call:

    $p1 = ORM::factory('product')
        ->where('product_type', '=', '1')
        ->find_all();

它将创建具有以下结构的对象:

it will create an object of this structure:

Model_Product Object
(
    [_long_list_of_kohana_properties] => Array ()
    [_object:protected] => Array
        (
            [id] => 2
            [product_type] => 1
            ...
            [product_attributes] => Array (List of attributes)
        )
)

当前,它产生以下内容:

Currently, it produces this:

Model_Product Object
(
    [_long_list_of_kohana_properties] => Array ()
    [_object:protected] => Array
        (
            [id] => 2
            [product_type] => 1
            ...
        )
)

这是对象和_has_many/_belongs_to的相关代码:

This is the relevant code for the objects and the _has_many / _belongs_to :

class Model_Product extends ORM
{
    protected $_db = 'default';
    protected $_table_name  = 'product';
    protected $_primary_key = 'id';

    protected $_table_columns = array(
        'id',
        'product_type',
        ...
    );

    protected $_has_many = array(
        'product_attributes'    => array(
            'model' => 'productAttributes',
            'foreign_key' => 'product_id',
            'far_key' => 'id',
        )
    );
}

class Model_ProductAttribute extends ORM
{
    protected $_db = 'default';
    protected $_table_name  = 'productAttributes';
    protected $_primary_key = 'id';

    protected $_table_columns = array(
        'id',
        'product_id',
        'attribute_name',
        'attribute_value',
    );

    protected $_belongs_to = array('product' => array(
            'model' => 'product',
            'foreign_key' => 'product_id',
            'far_key' => 'product_id',
        )
    );
}

我似乎无法正确地使用Foreign_key和far_key值的组合来完成这项工作……而且,我也找不到关于"far_key"用途的很好的解释.如果有人可以解释外国人还是远方的人可以解决这个问题,那就哈.

I can't seem to get the right combination of foreign_key and far_key values to make this work... Also, I can't find a good explanation of the purpose of "far_key". If someone can explain foreign vs far that might solve this problem, ha.

关于我可能会陷入困境的任何建议吗?

Any suggestions as to where I might be messing up?

谢谢.

推荐答案

Foreign keythis对象上的键.包含有关关系的数据.

Foreign key is the key on this object. That contains the data about the relationship.

Far keyother对象上的键.包含有关关系的数据.那个钥匙是遥远的"

Far key is the key on the other object. That contains the data about the relationship. That key is 'far away'

has_many关系中,其他对象属于"此对象.这些对象上都有一个引用this的键.因此,far key应该为'product_id',外键对此关系没有影响.因为this对象上没有(也不能是)键,所以它指向成千上万个属性对象.因此:

In a has_many relationship, the other objects 'belong to' this object. Those objects have a key on it which refers to this. Thus the far key should be 'product_id' and the foreign key has no influence on this relationship. As there is no (and can't be) a key on this object that points to thousands of attribute objects. Thus:

class Model_Product extends ORM {

    protected $_has_many = array(
        'product_attributes'    => array(
        'model' => 'productAttributes',
        'far_key' => 'product_id',
    ));
}

处于belongs_to关系中. this对象上有一个键,该键指向它的parent(或其他).因此,本地密钥(foreign_key)应该为product_id,而far_key对此关系没有影响.因为在另一个对象上没有(也不能是)一个指向其所有子项的键.因此:

In a belongs_to relationship. this object has a key on it which points to it's parent (or whatever). Thus the local key (foreign_key) should be product_id and the far_key has no influence on this relationship. As there is no (and can't be) a key on the other object that points to all it's children. Thus:

class Model_ProductAttribute extends ORM {

    protected $_belongs_to = array('product' => array(
        'model' => 'product',
        'foreign_key' => 'product_id',
        'far_key' => 'product_id',
    ));
}

我希望这能回答您的问题.也花了我一段时间才能弄清楚.

I hope this answered your question. Took me a while to figure it out as well.

这篇关于Kohana 3.3 ORM _has_many _belongs_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆