Kohana 3.3 ORM _has_many _belongs_to

不问归期 提交于 2019-12-20 07:39:11

问题


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
            ...
        )
)

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',
        )
    );
}

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?

Thank you in advance.


回答1:


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

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

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',
    ));
}

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.



来源:https://stackoverflow.com/questions/18067064/kohana-3-3-orm-has-many-belongs-to

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