Skip to content

Class Phalcon\Mvc\Model\Transaction

implements Phalcon\Mvc\Model\TransactionInterface

Source on GitHub

Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action. Phalcon\Transaction is intended to be used with Phalcon_Model_Base. Phalcon Transactions should be created using Phalcon\Transaction\Manager.

<?php

try {
    $manager = new \Phalcon\Mvc\Model\Transaction\Manager();

    $transaction = $manager->get();

    $robot = new Robots();

    $robot->setTransaction($transaction);

    $robot->name       = "WALL·E";
    $robot->created_at = date("Y-m-d");

    if ($robot->save() === false) {
        $transaction->rollback("Can't save robot");
    }

    $robotPart = new RobotParts();

    $robotPart->setTransaction($transaction);

    $robotPart->type = "head";

    if ($robotPart->save() === false) {
        $transaction->rollback("Can't save robot part");
    }

    $transaction->commit();
} catch(Phalcon\Mvc\Model\Transaction\Failed $e) {
    echo "Failed, reason: ", $e->getMessage();
}

Methods

public __construct (Phalcon\DiInterface $dependencyInjector, [boolean $autoBegin], [string $service])

Phalcon\Mvc\Model\Transaction constructor

public setTransactionManager (Phalcon\Mvc\Model\Transaction\ManagerInterface $manager)

Sets transaction manager related to the transaction

public begin ()

Starts the transaction

public commit ()

Commits the transaction

public boolean rollback ([string $rollbackMessage], [Phalcon\Mvc\ModelInterface $rollbackRecord])

Rollbacks the transaction

public getConnection ()

Returns the connection related to transaction

public setIsNewTransaction (mixed $isNew)

Sets if is a reused transaction or new once

public setRollbackOnAbort (mixed $rollbackOnAbort)

Sets flag to rollback on abort the HTTP connection

public isManaged ()

Checks whether transaction is managed by a transaction manager

public getMessages ()

Returns validations messages from last save try

public isValid ()

Checks whether internal connection is under an active transaction

public setRollbackedRecord (Phalcon\Mvc\ModelInterface $record)

Sets object which generates rollback action


Class Phalcon\Mvc\Model\Transaction\Exception

extends class Phalcon\Mvc\Model\Exception

implements Throwable

Source on GitHub

Methods

final private Exception __clone () inherited from Exception

Clone the exception

public __construct ([mixed $message], [mixed $code], [mixed $previous]) inherited from Exception

Exception constructor

public __wakeup () inherited from Exception

...

final public string getMessage () inherited from Exception

Gets the Exception message

final public int getCode () inherited from Exception

Gets the Exception code

final public string getFile () inherited from Exception

Gets the file in which the exception occurred

final public int getLine () inherited from Exception

Gets the line in which the exception occurred

final public array getTrace () inherited from Exception

Gets the stack trace

final public Exception getPrevious () inherited from Exception

Returns previous Exception

final public Exception getTraceAsString () inherited from Exception

Gets the stack trace as a string

public string __toString () inherited from Exception

String representation of the exception


Class Phalcon\Mvc\Model\Transaction\Failed

extends class Phalcon\Mvc\Model\Transaction\Exception

implements Throwable

Source on GitHub

This class will be thrown to exit a try/catch block for isolated transactions

Methods

public __construct (mixed $message, [Phalcon\Mvc\ModelInterface $record])

Phalcon\Mvc\Model\Transaction\Failed constructor

public getRecordMessages ()

Returns validation record messages which stop the transaction

public getRecord ()

Returns validation record messages which stop the transaction

final private Exception __clone () inherited from Exception

Clone the exception

public __wakeup () inherited from Exception

...

final public string getMessage () inherited from Exception

Gets the Exception message

final public int getCode () inherited from Exception

Gets the Exception code

final public string getFile () inherited from Exception

Gets the file in which the exception occurred

final public int getLine () inherited from Exception

Gets the line in which the exception occurred

final public array getTrace () inherited from Exception

Gets the stack trace

final public Exception getPrevious () inherited from Exception

Returns previous Exception

final public Exception getTraceAsString () inherited from Exception

Gets the stack trace as a string

public string __toString () inherited from Exception

String representation of the exception


Class Phalcon\Mvc\Model\Transaction\Manager

implements Phalcon\Mvc\Model\Transaction\ManagerInterface, Phalcon\Di\InjectionAwareInterface

Source on GitHub

A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them.

This class manages the objects that compose a transaction. A transaction produces a unique connection that is passed to every object part of the transaction.

<?php

try {
   use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager;

   $transactionManager = new TransactionManager();

   $transaction = $transactionManager->get();

   $robot = new Robots();

   $robot->setTransaction($transaction);

   $robot->name       = "WALL·E";
   $robot->created_at = date("Y-m-d");

   if ($robot->save() === false){
       $transaction->rollback("Can't save robot");
   }

   $robotPart = new RobotParts();

   $robotPart->setTransaction($transaction);

   $robotPart->type = "head";

   if ($robotPart->save() === false) {
       $transaction->rollback("Can't save robot part");
   }

   $transaction->commit();
} catch (Phalcon\Mvc\Model\Transaction\Failed $e) {
   echo "Failed, reason: ", $e->getMessage();
}

Methods

public __construct ([Phalcon\DiInterface $dependencyInjector])

Phalcon\Mvc\Model\Transaction\Manager constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injection container

public getDI ()

Returns the dependency injection container

public setDbService (mixed $service)

Sets the database service used to run the isolated transactions

public string getDbService ()

Returns the database service used to isolate the transaction

public setRollbackPendent (mixed $rollbackPendent)

Set if the transaction manager must register a shutdown function to clean up pendent transactions

public getRollbackPendent ()

Check if the transaction manager is registering a shutdown function to clean up pendent transactions

public has ()

Checks whether the manager has an active transaction

public get ([mixed $autoBegin])

Returns a new \Phalcon\Mvc\Model\Transaction or an already created once This method registers a shutdown function to rollback active connections

public getOrCreateTransaction ([mixed $autoBegin])

Create/Returns a new transaction or an existing one

public rollbackPendent ()

Rollbacks active transactions within the manager

public commit ()

Commits active transactions within the manager

public rollback ([boolean $collect])

Rollbacks active transactions within the manager Collect will remove the transaction from the manager

public notifyRollback (Phalcon\Mvc\Model\TransactionInterface $transaction)

Notifies the manager about a rollbacked transaction

public notifyCommit (Phalcon\Mvc\Model\TransactionInterface $transaction)

Notifies the manager about a committed transaction

protected _collectTransaction (Phalcon\Mvc\Model\TransactionInterface $transaction)

Removes transactions from the TransactionManager

public collectTransactions ()

Remove all the transactions from the manager


Interface Phalcon\Mvc\Model\Transaction\ManagerInterface

Source on GitHub

Methods

abstract public has ()

...

abstract public get ([mixed $autoBegin])

...

abstract public rollbackPendent ()

...

abstract public commit ()

...

abstract public rollback ([mixed $collect])

...

abstract public notifyRollback (Phalcon\Mvc\Model\TransactionInterface $transaction)

...

abstract public notifyCommit (Phalcon\Mvc\Model\TransactionInterface $transaction)

...

abstract public collectTransactions ()

...


Interface Phalcon\Mvc\Model\TransactionInterface

Source on GitHub

Methods

abstract public setTransactionManager (Phalcon\Mvc\Model\Transaction\ManagerInterface $manager)

...

abstract public begin ()

...

abstract public commit ()

...

abstract public rollback ([mixed $rollbackMessage], [mixed $rollbackRecord])

...

abstract public getConnection ()

...

abstract public setIsNewTransaction (mixed $isNew)

...

abstract public setRollbackOnAbort (mixed $rollbackOnAbort)

...

abstract public isManaged ()

...

abstract public getMessages ()

...

abstract public isValid ()

...

abstract public setRollbackedRecord (Phalcon\Mvc\ModelInterface $record)

...