Getting started

Models

Views and routes

Forms

SQL queries

Sessions and security

Plugins

Documentation

General principles of models

Model in MV framework is PHP class and SQL table managed by this class.

Main 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 project.
  3. The PHP class of model should be inherited from "Model" class.
  4. The model class should have a property called "$name", which is the text 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 components which model fields from "$model_elements" property are based on.
  7. SQL table of the Model should have the same name as a model's class name but 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 SQL 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" file, where the model name should be added into "$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" file.

<?
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;

After the 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 a few record for a welcome page and some inside pages.

Unlike "Pages" and "Blocks", "SEO" model is a Simple Model, which contains data in a key-value pairs. 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)