Esempio n. 1
0
 public function saveAction()
 {
     $this->db->begin();
     $robot = new Robots();
     $robot->name = "WALL·E";
     $robot->created_at = date("Y-m-d");
     if ($robot->save() == false) {
         $this->db->rollback();
         return;
     }
     $robotPart = new RobotParts();
     $robotPart->robots_id = $robot->id;
     $robotPart->type = "head";
     if ($robotPart->save() == false) {
         $this->db->rollback();
         return;
     }
     $this->db->commit();
 }
<?php

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();
}