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

发布于 2024-09-16 01:16:04 字数 445 浏览 4 评论 0原文

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

该站点将通过资源 uid 或 uri 别名查找页面并返回最新发布的修订版本。但是,用户可以从 uri 中将页面回滚到较早的修订版本,方法是在 uri 中包含日期上限或使用 -# 回滚 # 个修订版本。

因此,页面控制器将获取资源 uid(可能是日期)和修订回滚计数,从模型请求适当的记录,并将适当的记录传递到视图。

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

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

I am putting together a DB driven website with kohana and I need to be able to track revisions. So I have the data behind the individual pages in two tables. The first is the generic entity table that I use for keeping track of all the sites content. It contains basic information: resource uid, uri alias, creating user, creation date, and publishing user, and content model. The revision table contains rev_id, resource uid as FK, title, page content, revision creator, revision date, and publish approver.

The site will lookup a page by resource uid or uri alias and return the most recent published revision. However from the uri the user can roll back the page to earlier revisions by including an upper date limit in the uri or a -# to roll back # revisions.

So the page controller will be taking in the resource uid, possibly a date, and a revision rollback count, requesting the appropriate record from the models, and passing the appropriate record to the view.

Creating new pages will update both tables, updating pages will update one table, and deleting tables will effect 1 table.

Should I be creating two models, the entity model and the revision model? Or should I just have the one logical model that abstracts the actual structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2024-09-23 01:16:04

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

看起来像这样:

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

I had similar problem, and chose the way with two models.
Sorry for code sample, i wrote it only for illustration purposes.
To avoid problems with db structure changes, i just inherited revision model from entity model, and overloaded parent fields (or just unsetted them if i didnt need them).

Looked like this:

<?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 Excuse my english

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文