开发者

Kohana model for data spread across multiple tables

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 开发者_StackOverflow社区models, the entity model and the revision model? Or should I just have the one logical model that abstracts the actual structure?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜