We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Does Phalcon do implicit commints when saving Models?

I'm (trying to) wrap the saving of a model in a transaction, but even after a rollback, the new record still gets committed.

In my model, I begin a transaction in beforeSave(). Then in afterSave() I do some other stuff, then commit or rollback. The begin and rollback are being executed, but the query isn't rolled back.

My query log:

22934 Query     START TRANSACTION
22933 Query     INSERT INTO `interaction` (`id_person`, `date_created`, `date_edited`, `id_created`, `id_edited`, `int_type`, `status`) VALUES ('myUsername', DEFAULT, '2018-03-15 15:10:17', 'myUsername', 'myUsername', 7, DEFAULT)
22933 Query     SELECT IF(COUNT(*) > 0, 1, 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'interaction_corepie' AND `TABLE_SCHEMA` = DATABASE()
22933 Query     DESCRIBE `interaction_corepie`
22934 Query     ROLLBACK

The record created by that INSERT statement doesn't get rolled back.



12.8k
Accepted
answer
edited Mar '18

Yes, PDO use autocommit by default.
https://php.net/manual/en/pdo.transactions.php
You can try to disable autocommit in the PDO instance

$this->db->getInternalHandler()->setAttribute(PDO::ATTR_AUTOCOMMIT,0);

It looks like that worked. Thanks.

[Removed my initial reply because the cause of the problem stated in that reply was unrelated]