我正在为我的网站创建一个模块,这里我用hookActionProductUpdate钩子在ps_game_key表中插入了一些值。更新产品时,插入查询将执行两次。插入 SO 2 条记录,而不是插入 1 条记录。这似乎钩住了ActionProductUpdate被调用了两次。请帮忙
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);
}
}
钩子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;
}