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

How to Begin a transaction inside a model with a Database set in the DI with the phalcon bootstrap

I'm trying to do $this->db->begin(); But anyway I try I get or undefined function or No active transaction errors.

Just use the service associated to the model:

$di = \Phalcon\Di::getDefault();

$di->get('dbServiceName')->begin();

$di->get('dbServiceName')->commit();
edited May '15

It gives me There is no active transaction, i just have between begin and commit 3 lines of this->getModelsManager()->executeQuery("INSERT INTO ....");

The servicename if im not wrong is "db":

$di->set('db', function () use ($config) {
return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        'charset' => $config->database->charset,
    ));
});