我对Db :: getInstance()-> insert('product',$ dimension))做错了什么;没有将数据插入数据库?

时间:2018-09-18 13:22:03

标签: prestashop

我正在开发一个模块,该模块可以保存产品管理员的自定义字段。.但是我的代码没有将数据插入数据库。.下面是我的代码。

public function hookActionProductUpdate($params)
{
    if(Tools::isSubmit('submitDimension'))
    {
        $name = Tools::getValue('name');
        $length = Tools::getValue('custom_length');
        $width = Tools::getValue('custom_width');


        if(empty($name) || !Validate::isGenericName($name))
            $this->errors[] = $this->module->l('Invalid name');
        if(empty($length) || !Validate::isGenericName($length))
            $this->errors[] = $this->module->l('Invalid length');
        if(empty($width) || !Validate::isGenericName($width))
            $this->errors[] = $this->module->l('Invalid width');

            if(!$this->errors)
            {
            $dimension = array(
                'name' => $name,
                'custom_length' => $length,
                'custom_width' => $width

            );

            if(!Db::getInstance()->insert('product', $dimension));
                $this->errors[] = Tools::displayError('Error while updating database');
        }
    }
}

任何人都可以帮助我。.

这是我的安装功能

function install()
    {
        if (!parent::install() || 
            !$this->alterProductTable() ||
            !$this->registerHook('extraright') || 
            !$this->registerHook('displayAdminProductsExtra') || 
            !$this->registerHook('actionProductSave') ||
            !$this->registerHook('actionProductUpdate') || 
            !$this->registerHook('header')
            )
        return false;
        return true;
    }

这是我的alter table函数

 private function alterProductTable($method = 'add') 
        {
            if($method = 'add')
                $sql = '
                    ALTER TABLE '._DB_PREFIX_.'product 
                    ADD COLUMN `custom_length` decimal(20,6) NOT NULL, 
                    ADD COLUMN `custom_width` decimal(20,6) NOT NULL,
                    ADD COLUMN `name` VARCHAR(64) NOT NULL';

                if(!Db::getInstance()->Execute($sql))
                    return false;
                return true;
        }
.. the columns are there..

And here is my display admin hook
public function hookDisplayAdminProductsExtra($params)
    {
        $name = Db::getInstance()->getValue('SELECT `name` FROM '._DB_PREFIX_.'product WHERE id_product = '.Tools::getValue('id_product'));
        $length = Db::getInstance()->getValue('SELECT custom_length FROM '._DB_PREFIX_.'product WHERE id_product = '.(int)Tools::getValue('id_product'));
        $width = Db::getInstance()->getValue('SELECT custom_width FROM '._DB_PREFIX_.'product WHERE id_product = '.(int)Tools::getValue('id_product'));
        $this->context->smarty->assign(array(
            'name' => $name,
            'length' => $length,
            'width' => $width
        ));
        return $this->display(__FILE__, 'views/templates/hook/adminProductsExtra.tpl');
    }

我已经看了2天了。.我似乎找不到我做错了..我去了prestashop论坛,但到目前为止没有帮助..我希望我可以从这里的好人那里得到一些东西。预先感谢!

2 个答案:

答案 0 :(得分:0)

如果您没有查看Prestashop db最佳实践指南,请先检查。 这不是插入数据的正确方法,您还需要提及要在其中插入数据的特定表的字段。 Best Practices of the Db Class - Prestashop

答案 1 :(得分:0)

所有都是错误的,您试图将数据插入到一个表中,而该表具有许多其他必填字段,并且您还试图将数据插入到该表中不存在的列中。通过代码创建新产品的最佳方法是使用它们的对象,例如...

$product = new Product()
$product->id_tax_rules_group = 1;
$product->redirect_type = '404';
$product->name = array(
    $id_lang => 'Product name in this lang',
    $id_lang => 'Product name in this lang',
);
/*
 * And so on all the mandatory fields
 */
$product->save();