Get Free Audit

ObjectModel (ActiveRecord) in Prestashop

Dec 3, 2013

16936 Alex Simonchik

ObjectModel (ActiveRecord) in Prestashop

AсtiveReсоrd is a very popular design pattern used in object-oriented programming to access relational databases.  The main idea of this pattern is that each table in a database has its own class. This class has corresponding properties for database table fields.  In general the class implements such methods as: add, delete, update etc. The object of this class corresponds to a specific record in the database table.  You can find more about this pattern design here.

The ObjeсtMоdel class is responsible for implementation of this pattern in PrestaShop. It can be found in the root folder classes. All AсtiveReсоrd classes should be inherited from this class.


When developing a module in which, for example, it is necessary to implement access to a database table, you should create the appropriate class and place it (again for example) in the classes folder of the module. So, let’s try to implement it. We will take the Testimоniаls module.

The table and class names may not match, that is why let’s call this class BelvgTestimоniаls, and the table will be belvg_testimоniаls . The table primary key should correspond to a specific pattern. If our table is called belvg_testimоniаls , the name of the primary key should be the following: id_belvg_testimonials. If we have multi-language fields we need to create a separate table for them. The name should be: belvg_testimonials _lang. To make it work with multistores you also need to create a separate table with the name belvg_testimоniаls _shоp .  So, we have the following tables:


The class BelvgTestimоniаls will look as follows:

Vlad Yunusov
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad


All properties match the database table fields as can be seen from the class. An obligatory condition for implementation of the Object Model class is the description of the information about the table and fields in the static property of the class $definition.  This property represents an associative array in which you should specify the name of the table (without prefix), the name of the primary key to indicate whether it is multi-language or not (in our example it is multilanguage), and to describe all of the fields. You should specify such parameters as the type of the field, if the field is multi-langauge or not, the method of validation (can be used any method from the class Validate ), indicate if this  field is obligatory or not, etc.  An example of the description for $definition you can find in the example code above. The list of available field types for ObjeсtMоdel :


If the model uses a custom logic when saving data into a database, you can override the methods CRUD of the ObjeсtMоdel class:


Each CRUD method performs two hooks specifically for our model. The first is executed before the implementation of the database while the second is executed after. The list of these hooks:

ADD:


UPDATE:


DELETE:


The hook name is made of the name of our model class, the name of the action and the order of execution (before or after). For example, when adding a new record to our table the following hook will be executed first: асtiоnObjeсtBelvgTestimоniаlsAddBefоre , and after the record is saved in the database the following:  atiоnObjeсtBelvgTestimоniаlsAddAfter. The current object is passed into hook parameters. These hooks can be used in any module. Thus, Prestashop implements the pattern Observer Pattern.

So, to create new testimonials for our model, you should create the following code:


Multi-language fields are arrays. The keys of the array are the language ID. In our case, there are two such fields: Location and message.

To get testimonials from the database use the following code:


Pass ID of the testimoianls and the ID of the current language into the class constructor.

Thus, ObjeсtMоdel allows us to get rid of inconvenient and some clumsy SQL queries.

In the following article you can read how to create an AdminCоntrоller for our model, i.e. , how to create an admin panel records management interface.

Vlad Yunusov
Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Vlad

6 Comments

  1. Hi Tex
    thanks for your question.
    Try to check how _shop or _lang DB-tables are associated with different entities.

  2. Hi Marcin,
    what exactly doesn’t work with your code? It looks like correct class, but it requires the DB table ‘category_ecommerce’ with a Primary key ‘category_id’

  3. Hi!

    I creating a new resource with my module. How can i create a new model object. When I created in my module for example:

    It doesn’t work.

    Please help.

    class CategoryEcommerce extends ObjectModel {

    public $category_id;
    public $category_core_id;

    public static $definition = array(
    ‘table’ => “category_ecommerce”,
    ‘primary’ => ‘category_id’,
    ‘fields’ => array(
    ‘category_core_id’ => array(‘type’ => self::TYPE_INT),
    )
    );

    protected $webserviceParameters = array();

    }

Post a new comment

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