Jump to content

Recommended Posts

Buenos días a todo el mundo.

 

Publico en este foro lo mismo que en el foro de inglés, con vista a ver si alguien sabe ayudarme por aquí y para ayudar a alguien que pudiera tener el mismo problema

 

 

Tengo muchas dudas acerca de como desarrollar un módulo con las características de multi tienda y multi lengua.

 

Cuando usamos el método ObjectModel::add() tenemos lo siguiente (en la línea 489 de la clase ObjectModel de prestashop 1.7.1):

 

if (!$result = Db::getInstance()->insert($this->def['table'], $this->getFields(), $null_values)) {
    return false;
}

 

Si miramos la función getFields vemos lo siguiente (en la línea 251):

 

        // For retro compatibility
        if (Shop::isTableAssociated($this->def['table'])) {
            $fields = array_merge($fields$this->getFieldsShop());
        }

 

 

Aquí el script está mezclando dos arrays: el de campos específicos de la tienda y el de que no dependen de ella, devolviendo todos los campos en el mismo array. Así que luego, en el método insert que he copiado más arriba, está intentando insertar un grupo de atributos que no pertenencen a la tabla principal (la que no tiene el sufijo _shop).

 

A continuación os pego la definición de mi "ObjectModel":

 

    public static $definition = array(
        'table' => 'codeextracts',
        'primary' => 'id',
        // 'multishop' => true,
        // 'multilang' => true,
        'multilang' => true,
        'multilang_shop' => true,
        'fields' => array(
            'id' =>                 array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt''required' => false),
            'subreference' =>       array('type' => self::TYPE_STRING, 'validate' => 'isString''required' => true'lang' => TRUE'shop' => true),
            'blockreference' =>     array('type' => self::TYPE_STRING, 'validate' => 'isString''required' => true'lang' => TRUE'shop' => true),
            'text' =>               array('type' => self::TYPE_HTML, 'validate' => 'isString''required' => true'lang' => TRUE'shop' => true)
        ),
    );

 

Y este es el método con el que creo las tablas:

 

public static function createContentTable()
    {


        $sq1 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.self::$definition['table'].'`(
            `id` int(10) unsigned NOT NULL auto_increment,
            PRIMARY KEY (`id`)
            ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';


        $sq2 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.self::$definition['table'].'_shop`(
            `id` int(10) unsigned NOT NULL auto_increment,
            `id_shop` int(10) unsigned NOT NULL,
            PRIMARY KEY (`id`, `id_shop`)
            ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';



        $sq3 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.self::$definition['table'].'_lang`(
            `id` int(10) unsigned NOT NULL auto_increment,
            `id_lang` int(10) NOT NULL,
            `subreference` varchar(32) NOT NULL,
            `blockreference` varchar(32) NOT NULL,
            `text` text NOT NULL,
            PRIMARY KEY (`id`, `id_lang`), UNIQUE (`blockreference`, `subreference`)
            ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';

        $result = Db::getInstance()->execute($sq1
            && Db::getInstance()->execute($sq2
            && Db::getInstance()->execute($sq3);

        return $result;
    }

 

 

Alguien podría ayudarme y decirme que está mal?

 

Link to comment
Share on other sites

Cual es el problema, te crea las tablas correctamente al instalar el modulo ¿?

 

Hola Ventura.

 

Lo primero gracias por responder.

 

Las tablas las crea correctamente.

El problema surge, y no entiendo bien porque, cuando intenta insertar campos que no existen. Me explico. Hace un insert a la tabla general (que es codeextracts) e intenta meter en ella campos que corresponden a la tabla con sufijo shop, es decir (codeextracts_shop). Con lo cual al no tener esos campos en la tabla principal el sistema falla y lanza excepciones.

 

Un saludo.

Link to comment
Share on other sites

Creo que el problema viene de la function construct en el model del modulo, que no se como la tienes creada, deberÍa ser algo asi:

    public function __construct($id_primario = null, $id_lang = null, $id_shop = null)
    {
        Shop::addTableAssociation('tabla_del_modulo', array('type' => 'shop'));
        parent::__construct($id_primario, $id_lang, $id_shop);
    }

Desconozco si en la 1.7 esto ha cambiado.

Las funciones para crear y eliminar las tablas según se instale o desintale el modulo deberían estar mejor en el .php principal del modulo y ser llamadas desde las funciones install / uninstall

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...