Skip to content
Derek Jones edited this page Jul 5, 2012 · 8 revisions

Sometimes we need to create different objects but with the same behavior. For example, list of text blocks with possibility to reorder this list. And sometimes we need to create relations between entities. What should we do if our RDBMS does not support for example cascade deletion?

For simplification development I've created model which has basic methods for creating, deleting, moving up/down and updating.

Here is example showing how we can create entity like text blocks:

<?php
include_once("SchemeModel.php");

class TextBlockModel extends SchemeModel {
    
    function addTextBlock($title, $body) {
        return $this->_addObject("text_block", array("title" => $title, "body" => $body));
    }
    
    function updateTextBlock($id, $title, $body) {
        return $this->_updateObject("text_block", $id, array("title" => $title, "body" => $body));
    }
    
    function deleteTextBlock($id) {
        return $this->_deleteObject("text_block", $id);
    }
    
    function moveTextBlockUp($id) {
        $this->_moveObjectUp("text_block", $id);
    }
    
    function moveTextBlockDown($id) {
        $this->_moveObjectDown("text_block", $id);
    }
    
    function getTextBlockList() {
        $this->db->orderby("position");
        return $this->db->get("text_block");
    }
    
    function getTextBlock($id) {
        return $this->_getObject("text_block", $id);
    }
    
    function TextBlockModel() {
        parent::Model();
        
        $this->_setSchema(array(
            "text_block" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "title" => array(),
                    "body" => array(),
                    "position" => array("system" => true)
                ),
                "position" => true
            )
        ));
    }
}
?>

And here is more complex schema with relations:

        $this->_setSchema(array(
            "club" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "title" => array(),
                    "description" => array(),
                    "logo" => array(),
                    "hasflyer" => array("type" => "int"),
                    "bar_discount" => array("type" => "int")
                ),
                "relations" => array(
                    "club_genre" => "club_id",
                    "announcement" => "club_id",
                )
            ),
            "genre" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "title" => array()
                ),
                "relations" => array(
                    "club_genre" => "genre_id"
                )
            ),
            "club_genre" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "position" => array("system" => true),
                    "club_id" => array(),
                    "genre_id" => array()
                ),
                "position" => array("club_id")
            ),
            "announcement" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "club_id" => array("noupdate" => true),
                    "start" => array(),
                    "title" => array(),
                    "price" => array(),
                    "flyer_price" => array(),
                    "guests" => array(),
                    "lineup" => array(),
                    "hasflyer" => array("type" => "int"),
                    "isconcert" => array("type" => "int"),
                    "isrecomended" => array("type" => "int")
                ),
                "relations" => array(
                    "announcement_sms_action" => "announcement_id"
                )
            ),
            "sms_action" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "title" => array(),
                    "number" => array(),
                    "code" => array(),
                    "price" => array()
                ),
                "relations" => array(
                    "announcement_sms_action" => "sms_action_id"
                )
            ),
            "announcement_sms_action" => array(
                "fields" => array(
                    "id" => array("system" => true),
                    "sms_action_id" => array(),
                    "announcement_id" => array()
                ),
                "position" => array("announcement_id")
            )
        ));

And here is source of the Schema model: File:SchemeModel.zip

Clone this wiki locally