Kohana 3.3 ORM _has_many _belongs_to

前端 未结 1 2053
野性不改
野性不改 2021-01-28 01:51

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\')
        ->whe         


        
相关标签:
1条回答
  • 2021-01-28 02:40

    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.

    0 讨论(0)
提交回复
热议问题