Kohana模型的数据分布在多个表中

时间:2010-08-28 05:43:29

标签: model kohana data-modeling

我正在组建一个数据库驱动的网站和kohana,我需要能够跟踪修订。所以我在两个表中的各个页面背后有数据。第一个是我用来跟踪所有网站内容的通用实体表。它包含基本信息:资源uid,uri别名,创建用户,创建日期和发布用户以及内容模型。修订表包含rev_id,资源uid为FK,标题,页面内容,修订创建者,修订日期和发布批准者。

该网站将按资源uid或uri别名查找页面,并返回最新发布的修订版本。但是,从uri开始,用户可以通过在uri中包含上限日期限制来回滚页面到早期版本,或者使用# - 来回滚#revisions。

因此页面控制器将接收资源uid,可能是日期和修订回滚计数,从模型请求相应的记录,并将相应的记录传递给视图。

创建新页面将更新两个表,更新页面将更新一个表,删除表将影响1个表。

我应该创建两个模型,实体模型和修订模型吗?或者我应该只有一个抽象实际结构的逻辑模型?

1 个答案:

答案 0 :(得分:0)

我有类似的问题,并选择了两种模式的方式。 对不起代码示例,我写的只是为了说明目的。 为了避免db结构更改的问题,我只是从实体模型继承了修订模型,并且重载了父字段(或者如果我不需要它们,则只是取消它们)。

看起来像这样:

<?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请原谅我的英文