使用模块在Prestashop 1.7.6类别中添加自定义字段。如何保存在数据库中?

时间:2019-08-19 08:19:33

标签: prestashop prestashop-1.7

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

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 Class方法insert()更新数据库中的颜色字段(颜色字段创建良好)。

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

也许数据库方法不好?有人可以告诉我如何保存此数据吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我制作了这个模块,所以它可能很有用{{3}}

答案 2 :(得分:0)

尝试这样更新:

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