Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove EntityManager::transactional() #9278

Merged
merged 1 commit into from
Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 3.0

## BC BREAK: Remove `Doctrine\ORM\EntityManagerInterface#transactional()`

This method has been replaced by `Doctrine\ORM\EntityManagerInterface#wrapInTransaction()`.

## BC BREAK: Removed support for schema emulation.

The ORM no longer attempts to emulate schemas on SQLite.
Expand Down
24 changes: 0 additions & 24 deletions lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ObjectManagerDecorator;

use function get_class;
use function method_exists;
use function sprintf;
use function trigger_error;

use const E_USER_NOTICE;

/**
* Base class for EntityManager decorators
*/
Expand Down Expand Up @@ -52,28 +45,11 @@ public function beginTransaction()
$this->wrapped->beginTransaction();
}

/**
* {@inheritdoc}
*/
public function transactional($func)
{
return $this->wrapped->transactional($func);
}

/**
* {@inheritdoc}
*/
public function wrapInTransaction(callable $func)
{
if (! method_exists($this->wrapped, 'wrapInTransaction')) {
trigger_error(
sprintf('Calling `transactional()` instead of `wrapInTransaction()` which is not implemented on %s', get_class($this->wrapped)),
E_USER_NOTICE
);

return $this->wrapped->transactional($func);
}

return $this->wrapped->wrapInTransaction($func);
}

Expand Down
29 changes: 0 additions & 29 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ORM;

use BadMethodCallException;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\Common\EventManager;
use Doctrine\Common\Util\ClassUtils;
Expand Down Expand Up @@ -32,11 +31,9 @@
use Throwable;

use function array_keys;
use function call_user_func;
use function get_class;
use function gettype;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
use function ltrim;
Expand Down Expand Up @@ -221,32 +218,6 @@ public function getCache()
return $this->cache;
}

/**
* {@inheritDoc}
*/
public function transactional($func)
{
if (! is_callable($func)) {
throw new InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"');
}

$this->conn->beginTransaction();

try {
$return = call_user_func($func, $this);

$this->flush();
$this->conn->commit();

return $return ?: true;
} catch (Throwable $e) {
$this->close();
$this->conn->rollBack();

throw $e;
}
}

/**
* {@inheritDoc}
*/
Expand Down
21 changes: 1 addition & 20 deletions lib/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* EntityManager interface
*
* @method Mapping\ClassMetadataFactory getMetadataFactory()
* @method mixed wrapInTransaction(callable $func)
*/
interface EntityManagerInterface extends ObjectManager
{
Expand Down Expand Up @@ -72,24 +71,6 @@ public function getExpressionBuilder();
*/
public function beginTransaction();

/**
* Executes a function in a transaction.
*
* The function gets passed this EntityManager instance as an (optional) parameter.
*
* {@link flush} is invoked prior to transaction commit.
*
* If an exception occurs during execution of the function or flushing or transaction commit,
* the transaction is rolled back, the EntityManager closed and the exception re-thrown.
*
* @deprecated 2.10 Use {@link wrapInTransaction} instead.
*
* @param callable $func The function to execute transactionally.
*
* @return mixed The non-empty value returned from the closure or true instead.
*/
public function transactional($func);

/**
* Executes a function in a transaction.
*
Expand All @@ -106,7 +87,7 @@ public function transactional($func);
*
* @template T
*/
// public function wrapInTransaction(callable $func);
public function wrapInTransaction(callable $func);

/**
* Commits a transaction on the underlying database connection.
Expand Down
23 changes: 2 additions & 21 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,14 @@
</RedundantCastGivenDocblockType>
</file>
<file src="lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php">
<DeprecatedMethod occurrences="3">
<DeprecatedMethod occurrences="1">
<code>getHydrator</code>
<code>transactional</code>
<code>transactional</code>
</DeprecatedMethod>
<MissingParamType occurrences="3">
<code>$entity</code>
<code>$lockMode</code>
<code>$lockVersion</code>
</MissingParamType>
<MissingReturnType occurrences="1">
<code>wrapInTransaction</code>
</MissingReturnType>
<NonInvariantDocblockPropertyType occurrences="1">
<code>$wrapped</code>
</NonInvariantDocblockPropertyType>
Expand Down Expand Up @@ -479,9 +474,6 @@
<code>$this-&gt;repositoryFactory-&gt;getRepository($this, $entityName)</code>
<code>new $class($this)</code>
</LessSpecificReturnStatement>
<MissingReturnType occurrences="1">
<code>wrapInTransaction</code>
</MissingReturnType>
<MoreSpecificReturnType occurrences="2">
<code>EntityRepository&lt;T&gt;</code>
<code>newHydrator</code>
Expand Down Expand Up @@ -773,10 +765,9 @@
<code>$parent</code>
<code>new $definition['class']()</code>
</ArgumentTypeCoercion>
<DeprecatedMethod occurrences="3">
<DeprecatedMethod occurrences="2">
<code>addNamedNativeQuery</code>
<code>addNamedQuery</code>
<code>getName</code>
</DeprecatedMethod>
<DocblockTypeContradiction occurrences="2">
<code>! $definition</code>
Expand Down Expand Up @@ -875,9 +866,6 @@
<code>$class</code>
<code>$subclass</code>
</ArgumentTypeCoercion>
<DeprecatedMethod occurrences="1">
<code>canEmulateSchemas</code>
</DeprecatedMethod>
<DeprecatedProperty occurrences="4">
<code>$this-&gt;columnNames</code>
<code>$this-&gt;columnNames</code>
Expand Down Expand Up @@ -1040,10 +1028,6 @@
</PossiblyFalseOperand>
</file>
<file src="lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php">
<DeprecatedMethod occurrences="2">
<code>canEmulateSchemas</code>
<code>canEmulateSchemas</code>
</DeprecatedMethod>
<MissingClosureParamType occurrences="1">
<code>$joinColumn</code>
</MissingClosureParamType>
Expand Down Expand Up @@ -3312,9 +3296,6 @@
<RedundantConditionGivenDocblockType occurrences="1"/>
</file>
<file src="lib/Doctrine/ORM/Tools/SchemaTool.php">
<DeprecatedMethod occurrences="1">
<code>canEmulateSchemas</code>
</DeprecatedMethod>
<DocblockTypeContradiction occurrences="2">
<code>! $definingClass</code>
<code>$definingClass</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ public function beginTransaction()
$this->realEntityManager->beginTransaction();
}

/**
* {@inheritDoc}
*/
public function transactional($func)
{
return $this->realEntityManager->transactional($func);
}

/**
* {@inheritDoc}
*/
Expand Down
50 changes: 0 additions & 50 deletions tests/Doctrine/Tests/ORM/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,38 +240,6 @@ static function (EntityManagerInterface $em) use ($expectedValue) {
$this->assertSame($expectedValue, $return);
}

/**
* @group DDC-1125
*/
public function testTransactionalAcceptsReturn(): void
{
$return = $this->entityManager->transactional(static function ($em) {
return 'foo';
});

self::assertEquals('foo', $return);
}

public function testTransactionalAcceptsVariousCallables(): void
{
self::assertSame('callback', $this->entityManager->transactional([$this, 'transactionalCallback']));
}

public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected argument of type "callable", got "object"');

$this->entityManager->transactional($this);
}

public function transactionalCallback($em): string
{
self::assertSame($this->entityManager, $em);

return 'callback';
}

public function testCreateInvalidConnection(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -282,24 +250,6 @@ public function testCreateInvalidConnection(): void
EntityManager::create(1, $config);
}

/**
* @group #5796
*/
public function testTransactionalReThrowsThrowables(): void
{
try {
$this->entityManager->transactional(static function (): void {
(static function (array $value): void {
// this only serves as an IIFE that throws a `TypeError`
})(null);
});

self::fail('TypeError expected to be thrown');
} catch (TypeError $ignored) {
self::assertFalse($this->entityManager->isOpen());
}
}

/**
* @group #5796
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\ORMInvalidArgumentException;
Expand All @@ -22,7 +23,6 @@
use InvalidArgumentException;

use function get_class;
use function in_array;

class BasicFunctionalTest extends OrmFunctionalTestCase
{
Expand Down Expand Up @@ -717,7 +717,7 @@ public function testQueryEntityByReference(): void

$user->setAddress($address);

$this->_em->transactional(static function ($em) use ($user): void {
$this->_em->wrapInTransaction(static function (EntityManagerInterface $em) use ($user): void {
$em->persist($user);
});
$this->_em->clear();
Expand Down