Class Phalcon\Mvc\Model\Transaction

implements Phalcon\Mvc\Model\TransactionInterface

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.

  1. <?php
  2. try {
  3. $manager = new \Phalcon\Mvc\Model\Transaction\Manager();
  4. $transaction = $manager->get();
  5. $robot = new Robots();
  6. $robot->setTransaction($transaction);
  7. $robot->name = 'WALL·E';
  8. $robot->created_at = date('Y-m-d');
  9. if ($robot->save() == false) {
  10. $transaction->rollback("Can't save robot");
  11. }
  12. $robotPart = new RobotParts();
  13. $robotPart->setTransaction($transaction);
  14. $robotPart->type = 'head';
  15. if ($robotPart->save() == false) {
  16. $transaction->rollback("Can't save robot part");
  17. }
  18. $transaction->commit();
  19. } catch(Phalcon\Mvc\Model\Transaction\Failed $e) {
  20. echo 'Failed, reason: ', $e->getMessage();
  21. }

Methods

public __construct (unknown $dependencyInjector, [unknown $autoBegin], [unknown $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 ([unknown $rollbackMessage], [unknown $rollbackRecord])

Rollbacks the transaction

public Phalcon\Db\AdapterInterface getConnection ()

Returns the connection related to transaction

public setIsNewTransaction (unknown $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 (unknown $record)

Sets object which generates rollback action