Documentation

Class Phalcon\Mvc\Model\Transaction

extends abstract class Phalcon\Di\Injectable

implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, 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 boolean begin ()

Starts the transaction

public boolean commit ()

Commits the transaction

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

Rolls back the transaction

public Phalcon\Db\AdapterInterface getConnection ()

Returns the connection related to transaction

public setIsNewTransaction (boolean $isNew)

Sets if is a reused transaction or new once

public setRollbackOnAbort (boolean $rollbackOnAbort)

Sets flag to rollback on abort the HTTP connection

public boolean isManaged ()

Checks whether transaction is managed by a transaction manager

public array getMessages ()

Returns validations messages from last save try

public boolean isValid ()

Checks whether internal connection is under an active transaction

public setRollbackedRecord (Phalcon\Mvc\ModelInterface $record)

Sets object which generates rollback action

public Phalcon\Mvc\ModelInterface getRollbackedRecord ()

Gets object which generates rollback action

public setDI (Phalcon\DiInterface $dependencyInjector) inherited from Phalcon\Di\Injectable

Sets the dependency injector

public Phalcon\DiInterface getDI ([unknown $error], [unknown $notUseDefault]) inherited from Phalcon\Di\Injectable

Returns the internal dependency injector

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager) inherited from Phalcon\Di\Injectable

Sets the event manager

public Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\Di\Injectable

Returns the internal event manager

public boolean fireEvent (string $eventName, [mixed $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified

public mixed fireEventCancel (string $eventName, [mixed $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, can stop the event by returning to the false

public boolean hasService (string $name) inherited from Phalcon\Di\Injectable

Check whether the DI contains a service by a name

public Phalcon\Di\ServiceInterface setService (unknown $name) inherited from Phalcon\Di\Injectable

Sets a service from the DI

public object|null getService (unknown $name) inherited from Phalcon\Di\Injectable

Obtains a service from the DI

public mixed getResolveService (string $name, [array $args], [unknown $noerror], [unknown $noshared]) inherited from Phalcon\Di\Injectable

Resolves the service based on its configuration

public attachEvent (string $eventType, Closure $callback) inherited from Phalcon\Di\Injectable

Attach a listener to the events

public __get (unknown $property) inherited from Phalcon\Di\Injectable

Magic method __get

public __sleep () inherited from Phalcon\Di\Injectable

...

public __debugInfo () inherited from Phalcon\Di\Injectable

...