开发者

Kohana3 ORM: loading Key/Value Attributes

I have two MySQL-Tables (oversimplified):

articles
- id
- description

article_attributes
- article_id
开发者_JAVA技巧- attribute_name
- attribute_value

So, every article can have an unlimited amount of attributes. For the articles i have a Kohana_ORM Model

<?php class Model_Article extends ORM {} ?>

When loading the Model I would like to have access to all the attributes.

Example:

<?php
$article = new Model_Article(1); // or ORM::factory('article', 1);
echo $article->description;
echo $article->attribute->foo;
echo $article->attribute->bar;
?>

Could someone please point me in the right direction how to achive this?


I think that you need to create 2 model, one for each table, then define the relationship between them.

Try this,

class Model_Article extends ORM
{
    protected $_has_many = array('attributes' => array());
}

class Model_Atribute extends ORM
{
    protected $_table_name = 'article_attributes';
    protected $_belongs_to = array('article' => array());
}

Then you can do this,

$article = ORM::factory('article', 1);
$article_attributes = $article->attributes->find_all();

I don't know if you solved this issue or not, but I hope this answer helps.


You should read the Kohana ORM guide. It treats one to one, one to many (your case) and many to many relationships. I didn't copy pasted the code in here because there is quite a bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜