Jump to content

Issue with multilang and multishop objectmodel


Recommended Posts

Hello everyone, I've an issue with my module.
I'm using the Object Model associated with an Admin Controller to add custom “ranges” for the products that are multilingual and multishop.

I've followed the tutorial on the devdocs of PrestaShop 1.7 but everything I've tried got me the same result :
The data are inserting in all the table but not the main one (like in the attached picture).

Here is my Gamme model :

 

class Gamme extends ObjectModel
{
    public $id;

    /**
     * @var int $id_product_gamme ID of the gamme
     */
    public $id_product_gamme;

    /**
     * @var string $name Name of the gamme
     */
    public $name;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'product_gamme',
        'primary' => 'id_product_gamme',
        'multilang' => true,
        'multilang_shop' => true,
        'fields' => array(
            // Lang field
            'name' => array(
                'type' => self::TYPE_STRING,
                'lang' => true,
                'validate' => 'isGenericName',
                'required' => true,
                'size' => 255
            ),
        ),
    );

    //Write the add method here, it add a gamme to the database and return the id of the gamme added
    public function add($autodate = true, $null_values = false)
    {
        if (parent::add($autodate, $null_values)) {
            $this->id_product_gamme = (int)$this->id;
            return true;
        }

        return false;
    }

    /*
     * Get a list of gamme objects
     *
     * @param int $id_lang ID of the language to get the gamme names in (default: current language)
     * @param bool $active Whether to only retrieve active gamme (default: true)
     *
     * @return array Array of Gamme objects
     */
    public static function getGammes($id_lang = false, $id_shop = false)
    {
        $gammes = array();

        $sql = 'SELECT g.`id_product_gamme`
            FROM `' . _DB_PREFIX_ . 'product_gamme` g
            ' . Shop::addSqlAssociation('product_gamme', 'g') . '
            LEFT JOIN `' . _DB_PREFIX_ . 'product_gamme_lang` gl ON (g.`id_product_gamme` = gl.`id_product_gamme`' . Shop::addSqlRestrictionOnLang('gl') . ')
            LEFT JOIN `' . _DB_PREFIX_ . 'product_gamme_shop` gs ON (g.`id_product_gamme` = gs.`id_product_gamme`' . Shop::addSqlRestrictionOnLang('gs') . ')
            WHERE 1
            ' . ($id_lang ? 'AND gl.`id_lang` = ' . (int)$id_lang : '') . '
            ' . ($id_shop ? 'AND gs.`id_shop` = ' . (int)$id_shop : '') . '
            ORDER BY g.`id_product_gamme` ASC';

        $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);

        foreach ($results as $row) {
            $gamme = new Gamme($row['id_product_gamme'], $id_lang);
            $gammes[] = $gamme;
        }

        return $gammes;
    }
}


The controller :
 

require_once _PS_MODULE_DIR_ . 'elementfive/classes/Gamme.php';

class AdminElementFiveGammeController extends ModuleAdminController
{
    public function __construct()
    {
        Shop::addTableAssociation('product_gamme', ['type' => 'shop']);
        Shop::addTableAssociation('product_gamme_lang', ['type' => 'fk_shop']);

        $this->bootstrap = true;
        $this->table = Gamme::$definition['table'];
        $this->className = 'Gamme';
        $this->lang = true;
        $this->identifier = Gamme::$definition['primary'];

        $this->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
        $this->allow_employee_form_lang = (int) Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG');

        $this->context = Context::getContext();

        $this->fields_list = array(
            'id_product_gamme' => array(
                'title' => Context::getContext()->getTranslator()->trans('ID'),
                'align' => 'center',
                'class' => 'fixed-width-xs',
            ),
            'name' => array(
                'title' => Context::getContext()->getTranslator()->trans('Name'),
                'filter_key' => 'b!name',
                'lang' => true,
            ),
        );

        $this->bulk_actions = array(
            'delete' => array(
                'text' => Context::getContext()->getTranslator()->trans('Delete selected'),
                'confirm' => Context::getContext()->getTranslator()->trans('Would you like to delete the selected items?'),
            )
        );

        parent::__construct();
    }

    public function renderForm()
    {
        $this->fields_form = [
            'legend' => [
                'title' => $this->l('Gamme'),
                'icon' => 'icon-cogs'
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->l('Name'),
                    'name' => 'name',
                    'lang' => true,
                    'required' => true,
                    'col' => 4,
                    'hint' => $this->l('Invalid characters:') . ' <>;=#{}',
                    'empty_message' => $this->l('This field is required.'),
                ]
            ],
            'submit' => [
                'title' => $this->l('Save'),
                'name' => 'submitAdd' . $this->table,
            ]
        ];

        if (Shop::isFeatureActive()) {
            $this->fields_form['input'][] = [
                'type' => 'shop',
                'label' => $this->l('Shop association'),
                'name' => 'id_shop',
            ];
        }

        return parent::renderForm();
    }
}


Thanks for helping me, let me know if I have to add more information.

Capture d'écran 2023-04-04 105535.png

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...