Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

model.class.php

Tamas Szikszai edited this page Jan 14, 2014 · 5 revisions

model.class.php is your base class for creating your database models. It has some pre-defined methods to use or override if you wish:

  • public function validate($params, $update_id = false)

This is your class' validator method. $params is an associative array (keyed by your database fields). The optional $update_id field is used when you try to validate an update and it references the id of the existing row.

  • public function add($params)

Simply adds a new entry to the database. Before doing so it validates data

  • public function update($id, $params) Updates a row, before doing so validates the data

  • public function count($params = array())

Counts the rows matched by the $params criterias. At the moment it only supports "AND" relations

  • public function get($params = array(), $start = 0, $limit = 0, $order_field = "id", $order_direction = "DESC")

Retrieves a list of rows matching the criterias provided by $params

The soul of the model.class.php lies in the schema attribute. By default the schema is not defined, you have to define it when you extend the model class. Lets see it through an example:

class Product extends Model {

    public $db = null;
    public $schema = array( "id"    =>  array("type" => "int",
                                              "unique" => true,
                                              "notnull" => true,
                                              "nice_name" => "Id"),

                            "title" =>  array("type" => "string",
                                              "unique" => true,
                                              "required" => true,
                                              "notnull" => true,
                                              "nice_name" => "Product title"),

                            "thumbnail" =>  array("type" => "image",
                                                  "path" => "",
                                                  "url_prefix" => "",
                                                  "width" => "150",
                                                  "height" => "150",
                                                  "nice_name" => "Thumbnail"),

                            "category_id" => array("type" => "select",
                                                   "options" => array(),
                                                   "required" => true,
                                                   "notnull" => true,
                                                   "nice_name" => "Category"),

                            "description_short" =>  array("type" => "text",
                                                          "required" => true,
                                                          "notnull" => true,
                                                          "nice_name" => "Short description"),

                            "description_long" =>  array("type" => "text",
                                                          "required" => true,
                                                          "notnull" => true,
                                                          "nice_name" => "Long description"),
                            
                            "price" =>  array("type" => "float",
                                              "required" => true,
                                              "notnull" => true,
                                              "nice_name" => "Price"),

                            "recurring" => array("type" => "select",
                                                 "nice_name" => "Recurring period",
                                                 "default" => 0,
                                                 "options" => array("1" => "1 month",
                                                                    "2" => "2 months",
                                                                    "3" => "3 months",
                                                                    "4" => "4 months",
                                                                    "5" => "5 months",
                                                                    "6" => "6 months",
                                                                    "7" => "7 months",
                                                                    "8" => "8 months",
                                                                    "9" => "9 months",
                                                                    "10" => "10 months",
                                                                    "11" => "11 months",
                                                                    "12" => "12 months"))

                            );
    public $table = "products";
    public $nice_name = "Product";

    function __construct($db){
        global $baseurl, $basepath;

        $this->db = $db;
        $this->schema['thumbnail']['path'] = $basepath."/thumbs";
        $this->schema['thumbnail']['url_prefix'] = $baseurl."/thumbs";
    }
}

So in this example we extended model.class.php to fit our mysql table called "products". In the schema attribute we define each of our table fields one by one. These will also be our validation rules. Since Product extends Model it also inherits it's methods. It can also override the base methods easily like:

public function add($params){
    $params['title'] = strtolower($params['title']);
    return parent::add($params);
}
Clone this wiki locally