Login +

Models / Model Pattern

Each Model is automatically generated from its corresponding table in the database.

Supposea table "article" representing simple blog articles:

The tree generated is the following:

The formalism is as follows:

  • Business object : Alias_db.Table.model.php
  • SQL object : Alias_db.Table.sql.php

Model code generated is:

<?php
/**
 * Object Main_Article
 * @author SillySmart
 * @copyright SillySmart
 * @package Mvc.Models.Objects
 * @see Sls.Models.Core.SLS_FrontModel
 * @since 1.0
 */
class Main_Article extends SLS_FrontModel
{
    /**
     * Class variables
     */
    protected $__article_id;
    protected $__article_title;
    protected $__article_content;
    protected $__article_date;
    protected $__author_id;
    protected $_table = "article";
    protected $_db = "main";
    protected $_primaryKey = "article_id";
    public $_typeErrors = array();

    /**
     * Constructor Main_Article
     * @author SillySmart
     * @copyright SillySmart
     * @param bool $mutlilanguage true if multilanguage content, else false
     */
    public function __construct($multilanguage=false)
    {
        parent::__construct($multilanguage);
    }

    /**
     * Set the value of article_title
     * Errors can be catched with the public variable $this->_typeErrors['article_title']
     * @author SillySmart
     * @copyright SillySmart
     * @param string $value
     * @return bool true if updated, false if not
     */
    public function setArticleTitle($value)
    {
        if ($value === "")
        {
            $this->_typeErrors['article_title'] = "E_EMPTY";
            return false;
        }

        if (is_null($value))
        {
            $this->_typeErrors['article_title'] = "E_NULL";
            return false;
        }

        if ($this->__article_title == $value)
            return true;

        if (strlen($value) > 255)
        {
            $this->_typeErrors['article_title'] = "E_LENGTH";
            return false;
        }

        $this->__set('article_title', $value);
        return true;
    }

    /**
     * Set the value of article_content
     * Errors can be catched with the public variable $this->_typeErrors['article_content']
     * @author SillySmart
     * @copyright SillySmart
     * @param string $value
     * @return bool true if updated, false if not
     */
    public function setArticleContent($value)
    {
        if ($value === "")
        {
            $this->_typeErrors['article_content'] = "E_EMPTY";
            return false;
        }

        if (is_null($value))
        {
            $this->_typeErrors['article_content'] = "E_NULL";
            return false;
        }

        if ($this->__article_content == $value)
            return true;

        $this->__set('article_content', $value);
        return true;
    }

    /**
     * Set the value of article_date
     * Errors can be catched with the public variable $this->_typeErrors['article_date']
     * @author SillySmart
     * @copyright SillySmart
     * @param string $value
     * @return bool true if updated, false if not
     */
    public function setArticleDate($value)
    {
        if ($value === "")
        {
            $this->_typeErrors['article_date'] = "E_EMPTY";
            return false;
        }

        if (is_null($value))
        {
            $this->_typeErrors['article_date'] = "E_NULL";
            return false;
        }

        if ($this->__article_date == $value)
            return true;

        if (!SLS_Date::isDateTime($value))
        {
            $this->_typeErrors['article_date'] = "E_TYPE";
            return false;
        }

        $this->__set('article_date', $value);
        return true;
    }

    /**
     * Set the value of author_id
     * Errors can be catched with the public variable $this->_typeErrors['author_id']
     * @author SillySmart
     * @copyright SillySmart
     * @param int $value
     * @return bool true if updated, false if not
     */
    public function setAuthorId($value)
    {
        $this->_generic->useModel("Author","main");
        $object = new Main_Author();
        if ($object->getModel($value) === false)
        {
            $this->_typeErrors['author_id'] = "E_KEY";
            return false;
        }
        $this->__set('author_id', $value);
        return true;
    }

    /**
     * Get the instance of Author's Model
     * @author SillySmart
     * @copyright SillySmart
     * @return Main_Author $object the instance of Author's Model
     */
    public function author()
    {
        $this->_generic->useModel("Author","main");
        $object = new Main_Author();
        $object->getModel($this->__author_id);
        return $object;
    }
}
?>

The business object consists only of class variables describing the various columns of the table and specific setters to each column.

Each setter performs already classic audits "guessed" from SQL constraints: type checking, nullable, field lengths, unique constraints, etc.. You will see in the chapter on Administration of Models how to customize and extend these checks.

Each foreign key has its own access function corresponding to the model.

Related :

References

  1. SLS_Date - Tool SLS_Date - Dates Handling