public function testInnerTransaction() { foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $moscow = TestCity::dao()->getByLogic(Expression::eq('name', 'Moscow')); $piter = TestCity::dao()->getByLogic(Expression::eq('name', 'Saint-Peterburg')); $cityNewer = function (TestCity $city) { $city->dao()->merge($city->setName('New ' . $city->getName())); }; $citiesNewer = function ($moscow, $piter) use($cityNewer, $db) { $cityNewer($moscow); InnerTransactionWrapper::create()->setDB($db)->setFunction($cityNewer)->run($piter); }; InnerTransactionWrapper::create()->setDao($moscow->dao())->setFunction($citiesNewer)->run($moscow, $piter); $this->assertNotNull(TestCity::dao()->getByLogic(Expression::eq('name', 'New Moscow'))); $this->assertNotNull(TestCity::dao()->getByLogic(Expression::eq('name', 'New Saint-Peterburg'))); } }
/** * @throws WrongArgumentException * @return UnifiedContainer **/ public function save() { Assert::isArray($this->list, "that's not an array :-/"); if (!$this->fetched) { throw new WrongStateException('do not want to save non-fetched collection'); } $list = $this->list; $clones = $this->clones; $ids = $insert = $delete = $update = array(); if ($this->lazy) { foreach ($list as $id) { if (!isset($clones[$id])) { $insert[] = $ids[$id] = $id; } else { $ids[$id] = $id; } } foreach ($clones as $id) { if (!isset($ids[$id])) { $delete[] = $id; } } } else { foreach ($list as $object) { $id = $object->getId(); if (null === $id) { $insert[] = $object; } elseif (isset($clones[$id]) && $this->comparator->compare($object, $clones[$id]) != 0) { $update[] = $object; } elseif (!isset($clones[$id])) { $insert[] = $object; } if (null !== $id) { $ids[$id] = $object; } } foreach ($clones as $id => $object) { if (!isset($ids[$id])) { $delete[] = $object; } } } InnerTransactionWrapper::create()->setDao($this->getDao())->setFunction(array($this->worker, 'sync'))->run($insert, $update, $delete); $this->clones = array(); $this->syncClones(); $this->dao->uncacheLists(); return $this; }
public function testWrapRollbackByOtherException() { $db = $this->spawnDb(array('begin' => 1, 'rollback' => 1)); $exception = new DatabaseException('Some database exception'); $function = function () use($exception) { throw $exception; }; $wrapper = InnerTransactionWrapper::create()->setDB($db)->setFunction($function); try { $wrapper->run(); } catch (Exception $e) { $this->assertEquals($exception, $e); } }