我做错了Db :: getInstance() - > insert('product',$ dimension));不插入数据到数据库?

问题描述 投票:0回答:2

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

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论坛,但到目前为止没有帮助..我希望我能从这里的好人那里得到一些东西。提前致谢!

prestashop
2个回答
0
投票

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


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();
© www.soinside.com 2019 - 2024. All rights reserved.