From e3c0867b6be42f3ea50ed0c5388b609a66315bec Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 25 Oct 2013 16:36:25 +0200 Subject: [PATCH] Refactoring Table::save() to use Connection::transactional() --- Cake/ORM/Table.php | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index b4671bf8f09..0d0b69bcc70 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.php @@ -783,6 +783,19 @@ public function save(Entity $entity, array $options = []) { return $event->result; } + if ($options['atomic']) { + $connection = $this->connection(); + $success = $connection->transactional(function() use ($entity, $options) { + return $this->_processSave($entity, $options); + }); + } else { + $success = $this->_processSave($entity, $data, $options); + } + + return $success; + } + + protected function _processSave($entity, $options) { $data = empty($options['fieldList']) ? $entity->toArray() : $entity->extract($options['fieldList']); @@ -790,29 +803,16 @@ public function save(Entity $entity, array $options = []) { $schema = $this->schema(); $data = array_intersect_key($data, array_flip($schema->columns())); $query = $this->_buildQuery(); - - $connection = $this->connection(); - if ($options['atomic']) { - $connection->begin(); - } - - try { - $statement = $query->insert($this->table(), array_keys($data)) + $statement = $query->insert($this->table(), array_keys($data)) ->values($data) ->executeStatement(); - $success = false; - if ($statement->rowCount() > 0) { - $primary = $this->primaryKey(); - $id = $statement->lastInsertId($this->table(), $primary); - $entity->set($primary, $id); - $success = $entity; - } - } catch (\Exception $e) { - if ($options['atomic']) { - $connection->rollback(); - } - throw $e; + $success = false; + if ($statement->rowCount() > 0) { + $primary = $this->primaryKey(); + $id = $statement->lastInsertId($this->table(), $primary); + $entity->set($primary, $id); + $success = $entity; } if ($success) { @@ -820,10 +820,6 @@ public function save(Entity $entity, array $options = []) { $this->getEventManager()->dispatch($event); } - if ($options['atomic']) { - $connection->commit(); - } - return $success; }