Rushit
Rushit

Reputation: 526

How to write triggers in Magento?

I need to write the triggers in Magento module. The trigger codes works fine in MySQL but if the same is written in Magento Installer scripts it doesn't work. Can someone let me know how we can use trigger in Magento.

Trigger code:

DELIMITER ;;
CREATE TRIGGER `insert_user_trigger` BEFORE INSERT ON `sitealluser` FOR EACH ROW
BEGIN
    SET NEW.designs_created_at = NOW();
END;;
DELIMITER ;

Upvotes: 1

Views: 1607

Answers (2)

Ricardo Martins
Ricardo Martins

Reputation: 6003

If this the purpose is really to set a created_at = now() I would suggest to use a timestamp with CURRENT_TIMESTAMP as default value... It's easier and more elegant (in my opinion).

create table ...
(
        ...
        created_at timestamp default CURRENT_TIMESTAMP not null
);

Upvotes: 0

user439441
user439441

Reputation:

This setup script works:

$this->startSetup();

$this->run("CREATE TRIGGER `insert_user_trigger` BEFORE INSERT ON `sitealluser` FOR EACH ROW SET NEW.designs_created_at = NOW();");

$this->endSetup();

Check your SQL syntax.

Upvotes: 2

Related Questions