views:

33

answers:

2

I have two MySQL-Tables (oversimplified):

articles
- id
- description

article_attributes
- article_id
- 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?

+1  A: 

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.

mhitza
+4  A: 

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.

leonardys