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:
- The name of the PHP class must begin with the name of the model, for example: pages.model.php, gallery_main.model.php.
- The model class files must be located in the /models/ folder in the root of the project.
- The model class must be inherited from the Model class.
- The model class must have a $name property, which is the title of the model.
- 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.
- The fields in the $model_elements property of the model are based on data types.
- 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.
- 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.
- 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:
- Pages - menu pages
- Blocks - text blocks
- 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