Search code examples
prestashopprestashop-1.6prestashop-1.7

overriding already overridden classes with a module - Prestashop 1.6


I KNOW THIS QUESTION IS NOT SPECIFICALLY TIED TO PROGRAMMING. BUT I REALLY WANT TO UNDERSTAND PRESTASHOP'S BEHAVIOUR OF OVERRIDING FILES VIA MODULES.

I want to extend a MYSQL table, let's say, Orders. A raw sql query in Module's install method would get this, no problem. But I wanna add this column to Order Model as well, which is where the main problem would come. Since I must override it like this:

  public function __construct($id = null, $id_lang = null)
  {
    parent::__construct($id, $id_lang);
    self::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
  }

My question is, how do I make sure that if override directory already has an Order.php with a __construct, how do I make it to merge changes rather than throwing error.. Is it possible?


Solution

  • Prestashop doesn't encourage using of overrides within modules at all. Because how I know there is no way to merge two overrides and only the last one will work if you will find a way to install a module with the second override. So they recommend extending existing classes and add all necessary data from there. For example, in your case, it should be something like this

    class Order extends OrderCore
    {
        public $new_filed;
    
        public function __construct($id = null, $id_lang = null)
        {
            Order::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
            parent::__construct($id, $id_lang);
        }
    }
    

    and just include this class file inside your module. So if something similar will be included within another module no conflict should appear except if the properties will have the same name.