2016-04-21 20 views
0

我跟着this tutorial on creating a custom module for a new tab and input in the back office在prestahop中添加製表符和後臺產品 - 如何正確保存輸入?

我什麼都不想用語言做 - 我有一個簡單的文本輸入,其中用戶將輸入的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(); 
}  

當我點擊「保存&留」沒有任何反應。

我.tpl正顯示出它是這樣的:

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

任何幫助,將不勝感激。

總體目的只是讓用戶輸入數字並將其保存到產品後臺數據庫中的文本字段。

回答

0

我猜你的表稱爲ps_number_lines的列id_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); 
    } 
+0

要顯示後臺辦公室中的行數,是通過{$ number_lines}以.tpl中的適當方式訪問它? – Hanny

+0

如果已經指定了Smarty變量,是的,那很好,但是認爲您需要將其修改爲'{$ number_lines | intval}' –

0

你必須經過開發的相當廣泛的量「只是爲了有一個文本字段」 ^^

你必須在ps_product創建表的第一場(無符號整數)。

然後將您的字段添加到Product類成員(您需要先覆蓋產品類)。 內/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()功能內的產品。

+0

我鏈接到的教程沒有提及覆蓋。有沒有一種「最佳實踐」的方式來做到這一點(比如你的方法),還是隻是一種「無論什麼作品」? – Hanny

+0

如果您想爲此創建一個模塊,您可以使用掛鉤來完成。可能會更清潔。但是,您必須重寫產品模型,否則您將無法在自定義字段中註冊新值。 –

相關問題