开发者

Kohana 3 simple relations

开发者 https://www.devze.com 2022-12-31 12:14 出处:网络
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.

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/c开发者_如何学JAVAlasses/models/article.php first, yes?

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


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

0

精彩评论

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