2010-08-28 40 views
1

我正在使用ORM模塊構建一個使用Kohana 3.0.7的應用程序。我想創建一個對象,比如標籤,其中可以標記許多不同類型的對象,並且這些選項可以包含多個標籤。舉例來說,我有3個模型:標籤,發佈和頁面。我將如何構建表格和模型以使其效果最佳?在Kohana中使用多種對象類型建模多對多ORM

回答

3

你會使用Kohana的has-many-through關係。一個例子是:

class Model_Page 
{ 
    protected $_has_many = array(
     'tags' => array(
      'model' => 'tag', 
      'foreign_key' => 'page_id', 
      'far_key' => 'tag_id', 
      'through' => 'pages_tags', 
     ), 
    ); 
} 

class Model_Post 
{ 
    protected $_has_many = array(
     'tags' => array(
      'model' => 'tag', 
      'foreign_key' => 'post_id', 
      'far_key' => 'tag_id', 
      'through' => 'posts_tags', 
     ), 
    ); 
} 

class Model_Tag 
{ 
    protected $_has_many = array(
     'pages' => array(
      'model' => 'page', 
      'foreign_key' => 'tag_id', 
      'far_key' => 'page_id', 
      'through' => 'pages_tags', 
     ), 
     'posts' => array(
      'model' => 'post', 
      'foreign_key' => 'tag_id', 
      'far_key' => 'post_id', 
      'through' => 'posts_tags', 
     ), 
    ); 
} 
+1

+1提及Kohana文檔中缺少的'far_key' – Eloff 2013-02-05 12:32:24