I am using mysql and I want to create a trigger in Joomla 3.4.8.
I have a sql code that works in mySQLWorkbench:
If I run it in a Joomla controller:
$db = JFactory::getDbo();
$sql = $db->getQuery(true);
$sql = 'LOCK TABLES id0ap_virtuemart_products WRITE;
DROP TRIGGER IF EXISTS update_vm_products ;
DELIMITER //
CREATE TRIGGER update_vm_products
AFTER UPDATE
ON id0ap_virtuemart_products FOR EACH ROW
BEGIN
INSERT INTO id0ap_virtuemart_notifications
VALUES ("", NEW.virtuemart_product_id, "product","update");
END //
DELIMITER ;
UNLOCK TABLES;';
$db->setQuery($sql);
$db->execute();
I get:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // ' at line 2 SQL=LOCK TABLES id0ap_virtuemart_products WRITE; DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // CREATE TRIGGER update_vm_products AFTER UPDATE ON id0ap_virtuemart_products FOR EACH ROW BEGIN INSERT INTO id0ap_virtuemart_notifications VALUES ("", NEW.virtuemart_product_id, "product","update"); END // DELIMITER ; UNLOCK TABLES;
I can't find the problem. But I have dicovered something strange while I was trying to solve it. If I modify the sql deleting DELIMITERS and the DROP part:
$sql = 'CREATE TRIGGER update_vm_products
AFTER UPDATE
ON id0ap_virtuemart_products FOR EACH ROW
BEGIN
INSERT INTO id0ap_virtuemart_notifications
VALUES ("", NEW.virtuemart_product_id, "product","update");
END ;';
whether you just try to create the trigger it works and create the trigger, only fails when you add the DROP part. Any idea? Is there an object-oriented way to do this?
Thanks in advance!