hookActionProductUpdate在模块中调用了两次

时间:2014-11-28 04:25:38

标签: php mysql mysqli smarty prestashop

我为我的网站创建了一个模块,这里我使用了hookActionProductUpdate钩子在ps_game_key表中插入一些值。当产品更新时,插入查询执行两次.SO 2条记录被插入而不是插入1条记录。这似乎是hookActionProductUpdate被调用两次。请帮助

 class    customsupplier
{
  public function __construct()
  {
    $this->name = 'customsupplier';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'Rex';
    $this->need_instance = 0;

    parent::__construct();

    $this->displayName = $this->l('Custom Supplier');
    $this->description = $this->l('Custom Supplier Module');
 }

public function install()
{
    if (!parent::install() OR
        !$this->alterTable('add') OR            
        !$this->registerHook('actionAdminControllerSetMedia') OR
        !$this->registerHook('actionProductUpdate') OR
        !$this->registerHook('displayAdminProductsExtra'))
        return false;
    return true;
}


 public function hookActionProductUpdate($params)
    {


        Db::getInstance()->insert('game_key', array(
                'id_product' => 89,
                'key_type' =>'test',
                'game_key'      => 'reee',

                'added_date_time'=> 'ssd'
        ),true);

}

}

1 个答案:

答案 0 :(得分:1)

可能会多次触发挂钩product update,因为有很多地方会调用方法$product->save()

您可以随时以非常简单的方式阻止保存更多内容

protected $isSaved = false;

public function hookActionProductUpdate($params)
{
   if ($this->isSaved)
      return null;

   $isInsert = Db::getInstance()--insert(...);
   if ($isInsert)
      $this->isSaved = true;
}