MV framework logo
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Documentation
Download .zip version 3.2.0 from 25.12.2024
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
Documentation
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker

General principles of models

A model in MV is a PHP class and a SQL table managed by this class.
Basic rules for creating a model:

  1. The name of the PHP class must begin with the name of the model, for example: pages.model.php, gallery_main.model.php.
  2. The model class files must be located in the /models/ folder in the root of the project.
  3. The model class must be inherited from the Model class.
  4. The model class must have a $name property, which is the title of the model.
  5. The model class must have a $model_elements property, which is an array of fields of this model, similar to fields in a database table.
  6. The fields in the $model_elements property of the model are based on data types.
  7. The SQL table of the model must have the same name as the model class in lowercase pages, gallery_main. Tables are created using migrations.
  8. The names of the fields in the table must match the model fields in the $model_elements array, the name of the field in the SQL table is the third element of the parameters array.
  9. To activate the model, it must be specified in the config/models.php file, in this file the name of the model class must be added to the array of active models, after which the model will become available in the administrative interface and on the public part of the site.
//config/models.php file

$mvActiveModels = [
    'Pages', 'Blocks', 'Seo', 'GalleryMain'
];

An example of the simplest model

PHP class Hello should be in the file models/hello.model.php.

//models/hello.model.php file

class Hello extends Model
{
    protected $name = 'Hello world';

    protected $model_elements = [
        ['Name', 'char', 'name']
    ];
}

When installing MV, it contains 3 pre-installed models:

  1. Pages - menu pages
  2. Blocks - text blocks
  3. SEO - data for SEO optimization

The database already contains the necessary tables for these models. The pages table has several records for welcome pages.

Unlike Pages and Blocks, the SEO model is a simple model whose data is a key-value pair. Read more about this type of model in the Simple models section.

Below are 3 models linked by external keys.

News

class News extends Model
{
    protected $name = 'News';

    protected $model_elements = [
        ['Title', 'char', 'name', ['required' => true]],
        ['Section', 'enum', 'type', ['values_list' => ['economy' => 'Economy',
                                                       'sport' => 'Sport',
                                                       'culture' => 'Culture']]],
        ['Date', 'date', 'date'],
        ['Active', 'bool', 'active'],
        ['Text', 'text', 'content', ['rich_text' => true]],
        ['Comments', 'many_to_one', 'comments', ['related_model' => 'NewsComments',
                                                 'name_field' => 'author']]
    ];
}

News comments

class NewsComments extends Model
{
    protected $name = 'News comments';

    protected $name_field = 'author';

    protected $model_elements = [
        ['Author', 'enum', 'author', ['foreign_key' => 'Authors', 'name_field' => 'email']],
        ['Date', 'date_time', 'date'],
        ['News', 'enum', 'news_id', ['foreign_key' => 'News']],
        ['Text', 'text', 'content']
    ];
}

Comment authors

class Authors extends Model
{
    protected $name = 'Comment authors';

    protected $model_elements = [
        ['Email', 'email', 'email', ['required' => true, 'unique' => true]],
        ['Password', 'password', 'password'],
        ['Comments', 'many_to_one', 'comments', ['related_model' => 'NewsComments', 
                                                 'name_field' => 'date']],
        ['Photos', 'multi_images', 'pictures'],
        ['About me', 'text', 'about']
    ];
}

Previous section

Debugging

Next section

Data types
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github