使用模块在 Prestashop 1.7.6 类别中添加自定义字段.如何保存在数据库中? [英] Add custom field in Prestashop 1.7.6 category with a module. How to save in database?

查看:51
本文介绍了使用模块在 Prestashop 1.7.6 类别中添加自定义字段.如何保存在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Prestashop 1.7.6 中创建一个模块,以在我的类别管理页面中添加一些自定义字段.这是我的代码(来自 此来源 法语,使用了新的 Symfony 模型):

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);

        }

    }

在方法 updateData() 中,我使用类别 ID 和 lang ID 获取自定义字段,并使用 Db 类方法 insert() 更新数据库中的颜色字段(颜色字段已创建良好).

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

但是当我保存或更新时,出现此错误:[PrestaShopDatabaseException code 0].

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?

谢谢!

推荐答案

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

这篇关于使用模块在 Prestashop 1.7.6 类别中添加自定义字段.如何保存在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆