Skip to content

Commit

Permalink
Fixes issue with saveMany and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota committed Jun 14, 2018
1 parent f130611 commit ba4e523
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/ORM/Table.php
Expand Up @@ -2209,25 +2209,33 @@ protected function _update($entity, $data)
public function saveMany($entities, $options = [])
{
$isNew = [];

$return = $this->getConnection()->transactional(
function () use ($entities, $options, &$isNew) {
foreach ($entities as $key => $entity) {
$isNew[$key] = $entity->isNew();
if ($this->save($entity, $options) === false) {
return false;
}
}
}
);

if ($return === false) {
$cleanup = function ($entities) use (&$isNew) {
foreach ($entities as $key => $entity) {
if (isset($isNew[$key]) && $isNew[$key]) {
$entity->unsetProperty($this->getPrimaryKey());
$entity->isNew(true);
}
}
};

try {
$return = $this->getConnection()
->transactional(function () use ($entities, $options, &$isNew) {
foreach ($entities as $key => $entity) {
$isNew[$key] = $entity->isNew();
if ($this->save($entity, $options) === false) {
return false;
}
}
});
} catch (\Exception $e) {
$cleanup($entities);

throw $e;
}

if ($return === false) {
$cleanup($entities);

return false;
}
Expand Down

0 comments on commit ba4e523

Please sign in to comment.