【问题标题】:hookActionProductUpdate called two times in modulehookActionProductUpdate 在模块中调用了两次
【发布时间】:2014-11-28 04:25:38
【问题描述】:

我正在为我的网站创建一个模块,这里我使用 hookActionProductUpdate 钩子在 ps_game_key 表中插入一些值。当产品更新时,插入查询会执行两次。所以插入 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);

}

}

【问题讨论】:

    标签: php mysql mysqli smarty prestashop


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多