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

Correct Usage of TransactionManager

Hey guys,

so now I'm at the point, where I need to use a Transaction Manager.

Assume I have something like the following code:

    $item = Item::findFirst(...);
    $new = new Model1();

    if (!$new->create()) {
        // do rollback
    }

    $new2 = new Model2();

    ...

    if (!$new->create()) {
        // do rollback
    }

    if ($item-->getID() == 1) {
        if (!$item->delete()) {
            // do rollback.
        }
    } else {
        $item->setCount(1);

        if (!$item->save()) {
            // do rollback
        }
    }

Now is it enough to use Manual Transactions? Or should I use Isolated Transaction?

If it helps, this kind of Code is in nearly every Action or Library Function and when everything is done, it should be a Browsergame.

I'm extending phalcon mvc model with my model:

<?php
class Model extends \Phalcon\Mvc\Model
{
    public function create($data = null, $whiteList = null)
    {
        if (!parent::create($data, $whiteList)) {
            foreach ($this->getMessages() as $message) {
                throw new DbException($message);
            }
        }

        return $this;
    }

    public function update($data = null, $whiteList = null)
    {
        if (!parent::update($data, $whiteList)) {
            foreach ($this->getMessages() as $message) {
                throw new DbException($message);
            }
        }

        return $this;
    }

    public function save($data = null, $whiteList = null)
    {
            foreach ($this->getMessages() as $message) {
                throw new DbException($message);
            }
        }

        return $this;
    }

    public function delete()
    {
        if (!parent::delete()) {
            foreach ($this->getMessages() as $message) {
                throw new DbException($message);
            }
        }

        return $this;
    }
}

And when I need to do some actions in one transaction:

<?php

$this->db->begin();
try {
    $model = Model::findFirst(['id = ?0', 'bind' => [1], 'for_update' => true);
    $model->delete();

    $model2 = new Model2;
    $model2->create();

    $this->db->commit();
} catch (DbException $e) {
    $this->logException($e);
    $this->db->rollback();
}
edited Jun '18

This isn't exactly what I was asking for.

Manual Transaction or Isolated Transactions?

EDIT: Does no one have an advice for me?