1

我正在尝试在 kohana 3 web 框架中编写一个非常简单的 cms(用于学习目的)。我有我的数据库模式,我想将它映射到 ORM,但我遇到了关系问题。

模式:文章类别

一篇文章有​​一个类别。当然,一个类别可能有很多文章。

我认为它是文章表中的 has_one 关系。(?)

现在php代码。我需要先创建 application/classes/models/article.php,是吗?

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

1 回答 1

2
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)
 );

}

您也可以手动定义关系;

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

关于 Kohana 3 ORM 的更多信息

有关 Kohana ORM 命名约定的更多信息

于 2010-05-22T08:42:02.627 回答