Add custom field in Prestashop 1.7.6 category with a module. How to save in database?

余生长醉 提交于 2019-12-04 05:45:18

问题


I'm creating a module in Prestashop 1.7.6 to add some custom fields in my categories administration page. Here is my code (from this source in French, new Symfony model used) :

modules/categorycustomfields/categorycustomfields.php

    class Categorycustomfields extends Module
    {
        protected $config_form = false;

        public function __construct()
        {
            $this->name = 'categorycustomfields';
            $this->tab = 'administration';
            $this->version = '1.0.0';
            $this->author = 'abc';
            $this->need_instance = 0;

            $this->bootstrap = true;

            parent::__construct();

            $this->displayName = $this->l('Category Custom Fields');
            $this->description = $this->l('Add custom fields to category');

            $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
        }

        public function install()
        {
            if (!parent::install()
                // Install Sql du module
                || !$this->_installSql()
                //Installation des hooks
                || !$this->registerHook('actionAdminCategoriesControllerSaveAfter')
                || !$this->registerHook('actionAdminCategoriesFormModifier')
            ) {
                return false;
            }

            return true;
        }

        public function hookActionCategoryFormBuilderModifier(array $params)
        {
            //Récupération du form builder
            /** @var \Symfony\Component\Form\FormBuilder $formBuilder */
            $formBuilder = $params['form_builder'];


            //Ajout de notre champ spécifique
            $formBuilder->add('color',

                \Symfony\Component\Form\Extension\Core\Type\TextType::class,
                [
                    'label' => $this->l('Color'), //Label du champ
                    'required' => false, //Requis ou non
                    'constraints' => [ //Contraintes du champs

                        new \Symfony\Component\Validator\Constraints\Length([
                            'max' => 20,
                            'maxMessage' => $this->l('Max caracters allowed : 20'),
                        ]),
                    ],
                    'data' => '' //Valeur du champ
                ]
            );

            $formBuilder->setData($params['data'], $params);
        }

        public function hookActionAfterCreateCategoryFormHandler(array $params)
        {
            $this->updateData($params['form_data'], $params);
        }


        public function hookActionAfterUpdateCategoryFormHandler(array $params)
        {
            $this->updateData($params['form_data'], $params);
        }


      //params not well used but for examples
        protected function updateData(array $data, $params)
        {
            $insertData = array(
                'id_category'  => (int)$params['id'],
                'id_lang'  => (int)$this->context->language->id,
                'color'   => $data['color'],
            );
            //Update database
            Db::getInstance()->insert( "ps_category_lang ", $insertData);

        }

    }

In method updateData(), I get my custom field with the category ID and lang ID and I use the Db Class method insert() to update my color field in database (the color field is well created).

But when I save or update, I have this error : [PrestaShopDatabaseException code 0].

Maybe the database method is not good? Could somebody tell me how to save this data?

Thanks !


回答1:


$cat = new Category((int)$params['id']);
$cat->color= $data['color'];
$cat->update();



回答2:


Try to update like this:

$query = "UPDATE `"._DB_PREFIX_."category_lang` SET color='".$data['color']."' WHERE id_category = '".(int)$params['id']."' ";
Db::getInstance()->Execute($query);


来源:https://stackoverflow.com/questions/57552868/add-custom-field-in-prestashop-1-7-6-category-with-a-module-how-to-save-in-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!