views:

251

answers:

1

I'm trying to write a very simple cms (for learning purposes) in kohana 3 web framework. I have my db schemas and i want to map it to ORM but i have problems with relations.

Schemas:articles and categories

One article has one category. One category might has many articles of course.

I think it is has_one relationship in article table.(?)

Now php code. I need to create application/classes/models/article.php first, yes?

class Model_Article extends ORM
{
    protected // and i am not sure what i suppose to write here       
}
+1  A: 
class Model_Article extends ORM{

 protected $_belongs_to = array
 (
  'category'  => array(), // This automatically sets foreign_key to category_id and model to Model_Category (Model_$alias)
 );

}

class Model_Category extends ORM{

 protected $_has_many = array
 (
  'articles' => array(), // This automatically sets foreign_key to be category_id and model to Model_Article (Model_$alias_singular)
 );

}

You can also manually define the relation ;

'articles' => array('model'=>'article','foreign_key'=>'category_id');

More about Kohana 3 ORM

More about Kohana ORM naming conventions

Kemo