2010-05-22 13 views
1

J'essaie d'écrire un cms très simple (à des fins d'apprentissage) dans le cadre web kohana 3. J'ai mes schémas db et je veux le mapper à ORM mais j'ai des problèmes avec les relations.Kohana 3 relations simples

schemas: articles et categories

Un article a une catégorie. Une catégorie pourrait avoir de nombreux articles bien sûr.

Je pense qu'il est relation has_one dans le tableau de l'article. (?)

par code php. Je dois d'abord créer application/classes/models/article.php, oui?

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

Répondre

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

} 

Vous pouvez également définir manuellement la relation;

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

More about Kohana 3 ORM

More about Kohana ORM naming conventions