Skip to content

Commit

Permalink
Merge pull request #38 from Fooriva/master
Browse files Browse the repository at this point in the history
Wrap delete into transaction
  • Loading branch information
moufmouf committed Sep 18, 2017
2 parents 713fdba + 35d4045 commit ef5aef2
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/TDBMService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\VoidCache;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
Expand Down Expand Up @@ -244,7 +245,7 @@ public function setFetchMode($mode)
*
* @param AbstractTDBMObject $object the object to delete
*
* @throws TDBMException
* @throws DBALException
* @throws TDBMInvalidOperationException
*/
public function delete(AbstractTDBMObject $object)
Expand All @@ -265,21 +266,28 @@ public function delete(AbstractTDBMObject $object)
foreach ($object->_getDbRows() as $dbRow) {
$this->removeFromToSaveObjectList($dbRow);
}
// And continue deleting...
// And continue deleting...
case TDBMObjectStateEnum::STATE_NOT_LOADED:
case TDBMObjectStateEnum::STATE_LOADED:
$this->deleteManyToManyRelationships($object);
// Let's delete db rows, in reverse order.
foreach (array_reverse($object->_getDbRows()) as $dbRow) {
/* @var $dbRow DbRow */
$tableName = $dbRow->_getDbTableName();
$primaryKeys = $dbRow->_getPrimaryKeys();
$quotedPrimaryKeys = [];
foreach ($primaryKeys as $column => $value) {
$quotedPrimaryKeys[$this->connection->quoteIdentifier($column)] = $value;
$this->connection->beginTransaction();
try {
$this->deleteManyToManyRelationships($object);
// Let's delete db rows, in reverse order.
foreach (array_reverse($object->_getDbRows()) as $dbRow) {
/* @var $dbRow DbRow */
$tableName = $dbRow->_getDbTableName();
$primaryKeys = $dbRow->_getPrimaryKeys();
$quotedPrimaryKeys = [];
foreach ($primaryKeys as $column => $value) {
$quotedPrimaryKeys[$this->connection->quoteIdentifier($column)] = $value;
}
$this->connection->delete($this->connection->quoteIdentifier($tableName), $quotedPrimaryKeys);
$this->objectStorage->remove($dbRow->_getDbTableName(), $this->getObjectHash($primaryKeys));
}
$this->connection->delete($this->connection->quoteIdentifier($tableName), $quotedPrimaryKeys);
$this->objectStorage->remove($dbRow->_getDbTableName(), $this->getObjectHash($primaryKeys));
$this->connection->commit();
} catch (DBALException $e) {
$this->connection->rollBack();
throw $e;
}
break;
// @codeCoverageIgnoreStart
Expand Down

0 comments on commit ef5aef2

Please sign in to comment.