2010-08-28 39 views
1

我正在組建一個數據庫驅動的網站與kohana,我需要能夠跟蹤修訂。所以我有兩個表中各個頁面的數據。第一個是我用來跟蹤所有網站內容的通用實體表。它包含基本信息:資源uid,uri別名,創建用戶,創建日期和發佈用戶以及內容模型。修訂表包含rev_id,作爲FK的資源uid,標題,頁面內容,修訂創建者,修訂日期和發佈審批者。Kohana模型的數據分佈在多個表格

該網站將通過資源uid或uri別名來查找頁面,並返回最新發布的修訂。但是,從uri中,用戶可以通過在uri中包含上限日期限制或者在 - #中回滾#修訂版本,將頁面回滾到之前的修訂版本。

因此,頁面控制器將獲取資源uid,可能是日期和修訂回滾計數,從模型請求適當的記錄,並將適當的記錄傳遞給視圖。

創建新頁面將更新兩個表,更新頁面將更新一個表,並且刪除表將影響1個表。

我應該創建兩個模型,實體模型和修訂模型嗎?或者我應該擁有一個抽象出實際結構的邏輯模型?

+0

好問題。 – 2010-08-28 15:42:24

+0

查看ORM_Versioned類:http://dev.kohanaframework.org/projects/kohana2/repository/entry/tags/2.3.4/system/libraries/ORM_Versioned.php。可能是這個幫助 – biakaveron 2010-09-03 05:49:59

回答

0

我有類似的問題,並選擇了兩種模式的方式。 對不起,代碼示例,我寫它僅用於說明目的。 爲了避免數據庫結構變化的問題,我只是從實體模型繼承了修訂模型,並且重載了父域(或者如果我不需要它們,就將它們取消設置)。

是這樣的:

<?php 

// file /application/classes/model/page.php 
class Model_Page extends Jelly_Model { 

    public static function fields() { 
     return array(
      'id' => new Field_Primary(), 
      'content' => new Field_String(), 
      'author' => new Field_BelongsTo(array(
       'foreign' => 'user', 
       'column' => 'author_id'  
      )), 
      'creation_date' => new Field_Integer(), 
      'uri' => new Field_String() 

     ); 
    } 

    //here we init the model, using previously defined static method 
    public static function initialize(Jelly_Meta $meta){ 
     $meta->table('pages')->fields(self::fields()); 
    } 

} 

// file /application/classes/model/page/draft.php 
class Model_Page_Draft extends Model_Page { 
    public static function initialize(Jelly_Meta $meta) { 
     //here we inherit all the parent models' fields 
     //to skip the dirty work 
     $fields = parent::fields(); 
     //then we overload model propertires 
     //with fields relevant to draft model 
     $fields['rev_id'] = new Field_Integer(); 
     //and choose other table to work with 
     $meta->table('page_draft')->fields($fields); 


    } 
} 

PS請原諒我的英語