我在prestashop上触发了在产品缺货时更改产品的有效属性:
CREATE TRIGGER change_active AFTER UPDATE ON ps_stock_available
FOR EACH ROW
BEGIN
UPDATE
ps_product_shop
SET
active=0
WHERE
id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity=0);
END
它并没有给我任何错误,所以它应该有效但活动属性永远不会改变,即使我将产品的数量设置为零。
编辑:经过多次尝试,我发现在正确的更新后我的触发器没有被调用。这是我的代码,它现在完美无缺。
CREATE TRIGGER `change_active_after_update` AFTER UPDATE ON `ps_product`
FOR EACH ROW
BEGIN
UPDATE
ps_product_shop
SET
active=0
WHERE
id_product IN(SELECT id_product FROM ps_stock_available WHERE quantity=0);
END
答案 0 :(得分:1)
法规级触发器:
CREATE TRIGGER change_active ON ps_stock_available
FOR UPDATE
BEGIN
UPDATE
ps_product_shop
SET
active=0
FROM
inserted
WHERE
ps_product_shop.id_product = inserted.id_product and
inserted.quantity=0;
END
行级触发器:
CREATE TRIGGER change_active BEFORE UPDATE ON ps_stock_available
FOR EACH ROW
BEGIN
IF(NEW.quantity = 0) THEN
UPDATE
ps_product_shop
SET
active=0
WHERE
ps_product_shop.id_product = NEW.id_product;
END IF;
END