Mvc\Model::setTransaction

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

Sets a transaction related to the Model instance

use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;

try {

  $txManager = new TxManager();

  $transaction = $txManager->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("Robot part cannot be saved");
  }

  $transaction->commit();

} catch (TxFailed $e) {
  echo 'Failed, reason: ', $e->getMessage();
}
doc_Phalcon
2016-10-16 09:52:56
Comments
Leave a Comment

Please login to continue.