Last active
December 15, 2015 19:48
-
-
Save phalcon/5313498 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $eventsManager = new \Phalcon\Events\Manager(); | |
| $di = new \Phalcon\DI(); | |
| $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite([ | |
| "dbname" => "sample.db" | |
| ]); | |
| $connection->setEventsManager($eventsManager); | |
| $eventsManager->attach('db', | |
| function ($event, $connection) { | |
| switch ($event->getType()) { | |
| case 'beforeQuery': | |
| echo $connection->getSqlStatement(), '<br>'; | |
| break; | |
| case 'beginTransaction': | |
| echo 'BEGIN', '<br>'; | |
| break; | |
| case 'commitTransaction': | |
| echo 'COMMIT', '<br>'; | |
| break; | |
| case 'rollbackTransaction': | |
| echo 'ROLLBACK', '<br>'; | |
| break; | |
| } | |
| } | |
| ); | |
| //Setup a connection | |
| $di['db'] = $connection; | |
| //Set a models manager | |
| $di['modelsManager'] = new \Phalcon\Mvc\Model\Manager(); | |
| //Use the memory meta-data adapter or another | |
| $di['modelsMetadata'] = new \Phalcon\Mvc\Model\Metadata\Memory(); | |
| //$connection->execute('DROP TABLE robots; CREATE TABLE robots (id integer primary key, name varchar(120) not null, type varchar(32) not null, year int not null)'); | |
| class Robots extends \Phalcon\Mvc\Model | |
| { | |
| public $id; | |
| public $name; | |
| public $type; | |
| public $year; | |
| } | |
| function x($connection){ | |
| $robot = Robots::findFirst(); | |
| $robot->name = 'Hello'; | |
| $robot->save(); | |
| throw new Exception("Error Processing Request"); | |
| } | |
| try { | |
| $connection->begin(); | |
| x($connection); | |
| echo 'not here', '<br>'; | |
| $connection->commit(); | |
| } catch (\Exception $e) { | |
| // Rollback transaction | |
| $connection->rollback(); | |
| echo $e->getMessage(), '<br>'; | |
| } | |
| echo 'here', '<br>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment