2010-05-22 44 views
1

我想在kohana 3 web框架中編寫一個非常簡單的cms(用於學習目的)。 我有我的數據庫模式,我想映射到ORM,但我有關係的問題。Kohana 3簡單的關係

架構:articlescategories

一篇文章有​​一個類別。一類可能有許多文章,當然。

我認爲這是在文章表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  
} 

回答

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

More about Kohana 3 ORM

More about Kohana ORM naming conventions