在 prestashop 的后台产品中添加了选项卡和字段 - 如何正确保存输入?

Added tab & field to back office product in prestahop - how to save input properly?

我关注了this tutorial on creating a custom module for a new tab and input in the back office

我不想与语言有任何关系 - 我有一个简单的文本输入,用户将在其中输入 INT。

在教程中它让我们更新了 hookActionProductUpdate 方法:

public function hookActionProductUpdate($params)
{
// get the lines
// store the new field

$id_product = (int)Tools::getValue('id_product');
    if(!Db::getInstance()->update('number_lines', array('number_lines'=> pSQL(Tools::getValue('number_lines'))).' AND id_product = ' .$id_product ))
        $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
}    

当我点击 'save & stay' 时没有任何反应。

我的 .tpl 是这样显示的:

<div class="col-lg-1">
    <input type="text" name="number_lines" id="number_lines" value="{$number_lines|htmlentities}" />
</div>

如有任何帮助,我们将不胜感激。

总体目的只是有一个文本字段,用户可以在其中输入数字并将其保存到产品后台的数据库中。

你必须经历大量的开发"just to have a text field" ^^

您必须先在 ps_producttable 中创建字段(unsigned int)。

然后将您的字段添加到 Product class 成员(您需要先覆盖产品 class)。 里面 /override/classes/Product.php

class Product extends ProductCore
{
    public $number_lines;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        Product::$definition['fields']['number_lines'] = array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt');
        parent::__construct($id_product, $id_lang, $id_shop);
    }
...
}

然后您必须重写 AdminProductsController 以在表单中添加具有正确类型的字段。
最后,您必须在控制器的 postProcess() 函数中将字段分配给产品。

我猜你 table 调用了 ps_number_linesid_productnumber_lines,所以你的更新查询应该如下所示:

Db::getInstance()->update('number_lines', 
    array('number_lines' => pSQL((int)Tools::getValue('number_lines'))), 
    'id_product = ' . $id_product)

因为现在你用错了,为了更好地理解classes/db/Db.php

中的检查方法update
/**
     * @param string $table Table name without prefix
     * @param array $data Data to insert as associative array. If $data is a list of arrays, multiple insert will be done
     * @param string $where WHERE condition
     * @param int $limit
     * @param bool $null_values If we want to use NULL values instead of empty quotes
     * @param bool $use_cache
     * @param bool $add_prefix Add or not _DB_PREFIX_ before table name
     * @return bool
     */
    public function update($table, $data, $where = '', $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true)
    {
        if (!$data)
            return true;

        if ($add_prefix)
            $table = _DB_PREFIX_.$table;

        $sql = 'UPDATE `'.bqSQL($table).'` SET ';
        foreach ($data as $key => $value)
        {
            if (!is_array($value))
                $value = array('type' => 'text', 'value' => $value);
            if ($value['type'] == 'sql')
                $sql .= '`'.bqSQL($key)."` = {$value['value']},";
            else
                $sql .= ($null_values && ($value['value'] === '' || is_null($value['value']))) ? '`'.bqSQL($key)."` = NULL," : '`'.bqSQL($key)."` = '{$value['value']}',";
        }

        $sql = rtrim($sql, ',');
        if ($where)
            $sql .= ' WHERE '.$where;
        if ($limit)
            $sql .= ' LIMIT '.(int)$limit;
        return (bool)$this->q($sql, $use_cache);
    }