Get Free Audit

Prestashop MVC. Part 1: Creating a Model

Nov 15, 2016

77071 Dmitry Urbanovich

Prestashop MVC. Part 1: Creating a Model

This article is dedicated to the Model-view-controller in Prestashop CMS. Due to the amount of information we decided to divide it into 3 parts. In the following part we are going to consider basic principles of creation and operation with a model. Moreover, you can find some information regarding this subject under the following link.

Let’s consider Category(/classes/Category.php) object as a model example. Category class is inherited from ObjectModel:

Which in turn implements ERM (entity-relationship model) in Prestashop, in other words every row in database table subsumes to entity and object, that very simplifies data management.

Basing on Category class example we are going to create new tables in a database to build up our model:

In order of creating new tables it is necessary to follow some rules:

1. A name of unique table key is built in accordance with the rule:

2. If there are any data in different languages in the entity, then you must create a table name with postfix:

  • A table should contain at least 3 columns: ‘id_name_table’, ‘id_lang’, ‘id_shop’

3. In case entity has to work with multistore, it is necessary to create a table name with postfix:

  • A table should contain at least 2 columns: ‘id_name_table’, ‘id_shop’

4. In case you need to limit an access to the entity for different user groups, then you should create a table name with postfix: $name_table . ‘_group’ (CREATE TABLE IF NOT EXISTS ps_category_group …). Access restriction implementation is not inherited from ObjectModel class, and it will require to create additional methods in your entity. Let’s consider it below.

  • A table should contain at least 2 columns: ‘id_name_table’, ‘id_shop’

5. ‘id_name_table’ without AUTO_INCREMENT in all tables except of the first, because data could be duplicated and it may lead to exception.

Model Class:

As you can see from the example, each column of the table corresponds to a class property:

name varchar(128) NOT NULL, – public $name;

id_category int(10) unsigned NOT NULL, –  public $id_category;

To create a connection between model and tables it is necessary to specify the name of the table in $definition property, as well as the name of the unique key and determine whether our model will work with several languages:

Those fields that hold data in different languages should have ‘lang’ index with ‘true’ value:

And in case of using a multistore: ‘shop’ => true

It is necessary to add Shop::addTableAssociation() method in the model constructor, to configure the operation of model with multistore:

Let’s consider an implementation of permissions restriction by using ‘ps_category_group’ table:


Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Unfortunately, ObjectModel and controller implementation does not support its realization and you will have to add them by yourself.

As the result we are getting model class, in which the following methods are already determined:

  1. add($autodate = true, $nullValues = false) – Save current object to database (add or update).
  2. Delete() – Delete current object from database.
  3. save($nullValues = false, $autodate = true) – Save current object to database (add or update).
  4. update($nullValues = false) – Update current object to database.

A simple example of the model:

After the execution of a code in ‘ps_category’ table, in the string with ‘id_category’ = 12, ‘active’ field will change its value to opposite or will stay the same. ‘Name’ property, which is determined in ‘ps_category_lang’ table will be saved at the same place in the current language. But if you want to save it in several languages, do as follows:

Properties that are related to ‘ps_category_shop’ table will be saved automatically for the current store, but getting the value list from database will require to use Shop::addSqlAssociation() method. This method adds JOIN into sql request to connect entity fields selection from the current store.

And now let’s take a look how model operates with AdminController. AdminController is intended for data management in backend. Usually it displays lists of categories, products, editing forms. Look how easy it is to connect a module to controller (based on /controllers/admin/AdminCategoriesController.php):

Here we got 2 most important properties in controller to operate with a model:

And if you need to output entity list (rows of tables in your database) in your controller, then just specify the model properties names in $this->fields_list property:

It’s much easier to edit the data. Just create a form and make sure that fields names are identical to model properties names.

Creation, updating and filling out the form and list fields are performed by AdminController, and it does not require any development of additional logic of saving, deleting or updating.

Pre-save validation is performed by using the settings specified in ‘fields’ array of $definition model property  ( ‘active’ => array(‘type’ => self::TYPE_BOOL, ‘validate’ => ‘isBool’, ‘required’ => true),). It’s very important to name a unique key in the first table in accordance with the rule ‘id_’ . $name_table – ‘id_category’. This will help to avoid problems with data updating and deleting.

As we described above, the creation and using of a model in Prestashop is pretty easy process. Managing the model in admin area is rather simple and it doesn’t require monotypic implementation of CRUD.

Andrey Dubina

Partner With Us

Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *