简体   繁体   English

Kohana 3.3 ORM _has_many _belongs_to

[英]Kohana 3.3 ORM _has_many _belongs_to

I am trying to set up a product object in Kohana 3.3 using the built in ORM. 我正在尝试使用内置的ORM在Kohana 3.3中设置产品对象。 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
            ...
        )
)

This is the relevant code for the objects and the _has_many / _belongs_to : 这是对象的相关代码和_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',
        )
    );
}

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". 我似乎无法得到foreign_key和far_key值的正确组合来使这项工作......而且,我找不到对“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? 有关我可能搞砸的地方的任何建议吗?

Thank you in advance. 先感谢您。

Foreign key is the key on this object. Foreign keythis对象的关键。 That contains the data about the relationship. 其中包含有关关系的数据。

Far key is the key on the other object. Far keyother对象的关键。 That contains the data about the relationship. 其中包含有关关系的数据。 That key is 'far away' 那把钥匙'遥远'

In a has_many relationship, the other objects 'belong to' this object. has_many关系中,其他对象'属于'此对象。 Those objects have a key on it which refers to this . 那些对象上有一个关键字,指的是this Thus the far key should be 'product_id' and the foreign key has no influence on this relationship. 因此, far key应为'product_id' ,外键对此关系没有影响。 As there is no (and can't be) a key on this object that points to thousands of attribute objects. 由于this对象上没有(也可能不是)指向数千个属性对象的键。 Thus: 从而:

class Model_Product extends ORM {

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

In a belongs_to relationship. belongs_to关系中。 this object has a key on it which points to it's parent (or whatever). this对象上有一个键,指向它的parent (或其他)。 Thus the local key ( foreign_key ) should be product_id and the far_key has no influence on this relationship. 因此,本地密钥( foreign_key )应该是product_idfar_key对此关系没有影响。 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. 我花了一段时间才弄明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM