在prestashop

时间:2016-09-15 19:16:46

标签: model prestashop product prestashop-1.6

我的产品型号覆盖存在循环问题。我尝试实施此解决方案Update product from a module in prestashop。这是模型(具有用于测试目的的额外功能):

<?php
Class Product extends ProductCore
{
    // new field in product table
    public $design;
    public $fabric_front;
    public $fabric_back;
    public $fabric_front_border;
    public $fabric_back_border;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        // adding new fields in product's model definition
        self::$definition['fields']['design'] = array(
            'type' => self::TYPE_HTML, 
            'validate' => 'isCleanHtml',
        );
        self::$definition['fields']['fabric_front'] = array(
            'type' => self::TYPE_INT,
            'validate' => 'isUnsignedInt',
        );
        self::$definition['fields']['fabric_back'] = array(
            'type' => self::TYPE_INT,
            'validate' => 'isUnsignedInt',
        );
        self::$definition['fields']['fabric_front_border'] = array(
            'type' => self::TYPE_INT,
            'validate' => 'isUnsignedInt',
        );
        self::$definition['fields']['fabric_back_border'] = array(
            'type' => self::TYPE_INT,
            'validate' => 'isUnsignedInt',
        );

        // resume normal process
        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }

    public function setDesign()
    {
        $sql = 'UPDATE '._DB_PREFIX_.'product SET design="'.$this->design.'" WHERE id_product='.$this->id.';';
        if (!($update = Db::getInstance()->execute($sql)))
            return false;

        return true;
    }

}

这就是我在Hooks中调用模型的方式:

public function hookDisplayAdminProductsExtra($params)
{
    $id_product = (int)Tools::getValue('id_product');
    if (Validate::isLoadedObject($product = new Product($id_product))) {

        if (!empty($product) && isset($product->id)) 
            $this->context->smarty->assign(array(
                'design_svg_img' => $product->design,
            ));

        return $this->display(__FILE__,'views/templates/admin/build_design.tpl');

    } else 
        return $this->displayWarning($this->l('You must save this product before adding a design.'));
}

public function hookActionProductUpdate($params)
{
    // get params from template
    $id_product = (int)Tools::getValue('id_product');
    $svg_content = Tools::getValue('design_svg_upload');

    if (Validate::isLoadedObject($product = new Product($id_product))) {

        // attribute values to model members
        $product->design = $svg_content;

        if (!$product->save())
        // if (!$product->setDesign())
            return $this->displayWarning($this->l('Impossible to update product'));

    } else
        return $this->displayWarning($this->l('Product not found'));
}

机制工作,当我使用方法$ product-&gt; save()时,数据被插入并从数据库中检索,但它也在hookActionProductUpdate()上创建了一个无限循环。

但如果我使用自己的函数$ product-&gt; setDesign()代替,一切都很好。 所以很明显我错过了一些东西。但我没有看到它就连续2天。

0 个答案:

没有答案