General Principles of Models

Model in MV framework is PHP class and SQL table managed by this class.
General rules of Model:

  1. The name of PHP class should start with the name of the model, for example: "pages.model.php", "gallery.model.php".
  2. The files of model classes should be located in "/models/" folder in the root of the project.
  3. The model class should be inherited from "Model" class.
  4. The model class should have a property called "$name", which is the name of model.
  5. The model class should have a property called "$model_elements", which is an array of the fields for this model, the same as fields of table in a database.
  6. Data types are the main point of fields specification in "$model_elements" property.
  7. SQL table of the Model should have the same name as Model Class in lower registry, such as "pages", "gallery".
  8. The field names of the table are to be the same as model fields in "$model_elements" array. (the field name in SQL table is 3rd element of an array).
  9. The table should have an integer field called "id", which should be a primary key and has an "auto increment" attribute.
  10. To activate the model you should specify it in "config/models.php", in this file the model name should be added in "$mvActiveModels" array, after that the model becomes available both in Admin Interface and front-end part of the website.

Sample 1 Text blocks

"Blocks" PHP class located in "models/blocks.model.php".

<?
class Blocks extends Model
{
    protected $name = "Text blocks";

    protected $model_elements = array(
        array("Activation", "bool", "active"),
        array("Name", "char", "name"),
        array("Content", "text", "content")
    );
}
?>

SQL table in database with the same name "blocks" (MySQL).

CREATE TABLE `blocks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `active` tinyint(4),
  `name` varchar(200),
  `content` text,
   PRIMARY KEY (`id`),
)  ENGINE=MyISAM DEFAULT CHARSET=utf8;

Upon installation of MV there are 3 pre-installed models:

  1. Pages (menu pages)
  2. Blocks (text blocks, fragments)
  3. SEO (data for seo optimization)

The database already contains all the required tables for these models. "Pages" table has 1 record for a welcome page.

Unlike "Pages" and "Blocks", "SEO" Model is a Simple Model, which data is a key-value pair. Read more about this type of models in Simple Models section.

Sample 2 News

<?
class News extends Model 
{
    protected $name = "News";

    protected $model_elements = array(
        array("Active", "bool", "active"),
        array("Name", "char", "name", array("required" => true)),
        array("Section", "enum", "type", array("values_list" => array("politic" => "Politics",
                                                                      "economy" => "Economics",
                                                                      "sport" => "Sport",
                                                                      "culture" => "Culture"))),
        array("Date", "date", "date"),
        array("Text", "text", "content", array("rich_text" => true)),
        array("Comments", "many_to_one", "comments", array("related_model" => "News_Comments", 
                                                         "name_field" => "author"))
    );                    
}
?>
CREATE TABLE "news" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
  "name" VARCHAR, 
  "type" VARCHAR, 
  "date" VARCHAR, 
  "active" INTEGER, 
  "content" TEXT);

Sample 3 News Comments

<?
class News_Comments extends Model
{
    protected $name = "News comments";

    protected $model_elements = array(
        array("Author", "enum", "author", array("foreign_key" => "Authors", 
                                                "name_field" => "email")),
        array("Date", "date_time", "date"),
        array("News", "enum", "news_id", array("foreign_key" => "News")),        
        array("Text", "text", "content"),
    );

    protected $name_field = "author";
}
?>
CREATE TABLE "news_comments" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  "author" INTEGER,
  "date" VARCHAR,
  "news_id" INTEGER,  
  "content" TEXT);

Sample 4 Comments Authors

<?
class Authors extends Model
{
    protected $name = "Comments Authors";

    protected $model_elements = array(
        array("Name", "char", "name", array("required" => true)),
        array("Email", "email", "email", array("required" => true, "unique" => true)),
        array("Password", "password", "password"),
        array("Comments", "many_to_one", "comments", array("related_model" => "News_Comments",
                                                           "name_field" => "date")),
        array("Photos", "multi_images", "pictures"),
        array("About", "text", "about")
    );                    
}
?>
CREATE TABLE "authors" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  "name" VARCHAR,
  "email" VARCHAR, 
  "password" VARCHAR,
  "pictures" TEXT,
  "about" TEXT);