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

afterSave model event fired before transaction commit

Is there a way to fire afterCreate and afterSave events only after the transaction is commited succesfully?

// Controller
$t = new TransactionManager()->get()->throwRollbackException(true);

try {

   if (!$a->save()) {

// here the events for afterCreate / afterSave for model a are
// fired even thought the transaction was not commited

   if (!$b->save()) {

   $t->rollback('stop the transaction to test model events');


catch (Exception $e) {
   var_dump('if rollback, code should each here without running any afterSave event');
// Model
public function afterSave() {
   var_dump('should run after the data exists in the database');

I've never used either, but I wonder if getDirtyState() or getOperationMade(), could be called to see if the operaton completed successfully? Also, I'm not sure if getUpdatedFields() changes if the save failed.

If none of those work, maybe try wrapping save() to set a flag?

public function save(){
    $this->saved = parent::save();
    return $this->saved;
public function afterSave(){
        // do stuff

Thanks for the suggestion. I ended up using the EventManager to trigger a function after the transaction commit.