Menu Close
    Searching for an experienced eCommerce & Marketplace Development Company ?

    Add Custom Fields in Database Table by Module Override

    In PrestaShop, each entity in database is represented with an ObjectModel class. In this way we can interact with database tables without directly executing any MySQL query. Sometimes we need some extra fields in a database table to manage with module. We can add custom fields in database table by overriding Object Model class in our module.

    The steps to do this are as follows:

    Step 1 : Create custom field in table on module installation

    We need to create the custom fields as columns in the database table when the module is installing. To do this, we execute the add column query at the time of module installation. For example, we need to add a field “phone_number” to customer table. So we will override the install function and call the following query:

    Db::getInstance()->execute("ALTER TABLE `"._DB_PREFIX_."customer` ADD `phone_number` VARCHAR(15) NOT NULL DEFAULT '';");

    Step 2 : Overriding the Object Model class

    Next step is to override the corresponding Object Model class just like any other core class overrides. You can check here how to override a core class in PrestaShop.

    In this override file, we will add the necessary property to map the custom field added in the database table. Then we will add this field to ObjectModel definition by overriding the __construct function.

    According to our example, we will override Customer class and add “phone_number” column as a Class property:

    <?php
    
    class Customer extends CustomerCore
    {
        public $phone_number;
    
        public function __construct(
            $id = null,
            $idLang = null
        ) {
            parent::__construct($id, $idLang);
            // Add the custom field to the definition of this Object Model Class
            self::$definition['fields']['phone_number'] = ['type' => self::TYPE_STRING, 'size' => 15];
        }
    }
    

    We have successfully added the field in the database table and the Object Model class. Now we can use this property in the same Customer object while saving it. We do not need to call any subsequent query to manage this field in separate table for each customer.

    Step 3 : Remove the custom field on uninstallation

    It is also necessary to remove the field from database table on module uninstallation. This is to avoid any duplicate column type fatal issues if users try to reset the module. We need to execute a query to remove the custom field in uninstall function in our module file.

    To remove the “phone_number” field from customer table:

    
    
    Db::getInstance()->execute("ALTER TABLE `"._DB_PREFIX_."customer` DROP COLUMN `phone_number`;");

    That’s all about adding custom fields in database table by overriding object model Class. If any issue or doubt in the above process, please feel free to let us know in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top