From a118d4e0dce0ca1964f329d70bab1b411ea98095 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Fri, 13 Nov 2020 22:28:33 +0100 Subject: [PATCH] Use LockMode::NONE as default lock mode --- .../Entity/AbstractEntityPersister.php | 9 +++++---- .../ORM/Decorator/EntityManagerDecorator.php | 3 ++- lib/Doctrine/ORM/EntityManager.php | 9 +++------ lib/Doctrine/ORM/EntityRepository.php | 7 +++---- .../Entity/BasicEntityPersister.php | 8 ++++---- .../ORM/Persisters/Entity/EntityPersister.php | 20 +++++++++---------- .../Entity/JoinedSubclassPersister.php | 2 +- lib/Doctrine/ORM/Query.php | 6 +++--- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- lib/Doctrine/ORM/UnitOfWork.php | 1 - 10 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php index b357d288fbe..b02b11abab3 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php @@ -5,6 +5,7 @@ namespace Doctrine\ORM\Cache\Persister\Entity; use Doctrine\Common\Collections\Criteria; +use Doctrine\DBAL\LockMode; use Doctrine\ORM\Cache; use Doctrine\ORM\Cache\CollectionCacheKey; use Doctrine\ORM\Cache\EntityCacheKey; @@ -105,7 +106,7 @@ public function __construct(EntityPersister $persister, Region $region, EntityMa public function getSelectSQL( $criteria, ?AssociationMetadata $association = null, - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, $offset = null, array $orderBy = [] @@ -360,11 +361,11 @@ public function load( $entity = null, ?AssociationMetadata $association = null, array $hints = [], - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, ?array $orderBy = null ) { - if ($entity !== null || $association !== null || ! empty($hints) || $lockMode !== null) { + if ($entity !== null || $association !== null || ! empty($hints)) { return $this->persister->load($criteria, $entity, $association, $hints, $lockMode, $limit, $orderBy); } @@ -642,7 +643,7 @@ public function lock(array $criteria, $lockMode) /** * {@inheritdoc} */ - public function refresh(array $id, $entity, $lockMode = null) + public function refresh(array $id, $entity, $lockMode = LockMode::NONE) { $this->persister->refresh($id, $entity, $lockMode); } diff --git a/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php b/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php index e4c844ebb4e..5070533c07d 100644 --- a/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php +++ b/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php @@ -5,6 +5,7 @@ namespace Doctrine\ORM\Decorator; use Doctrine\Common\Persistence\ObjectManagerDecorator; +use Doctrine\DBAL\LockMode; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Query\ResultSetMapping; @@ -136,7 +137,7 @@ public function lock($entity, $lockMode, $lockVersion = null) /** * {@inheritdoc} */ - public function find($entityName, $id, $lockMode = null, $lockVersion = null) + public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion = null) { return $this->wrapped->find($entityName, $id, $lockMode, $lockVersion); } diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 661161ea4b1..6cc8abe9187 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -362,9 +362,7 @@ public function flush() * * @param string $entityName The class name of the entity to find. * @param mixed $id The identity of the entity to find. - * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants - * or NULL if no specific lock mode should be used - * during the search. + * @param int $lockMode One of the \Doctrine\DBAL\LockMode::* constants. * @param int|null $lockVersion The version of the entity to find when using * optimistic locking. * @@ -375,12 +373,12 @@ public function flush() * @throws TransactionRequiredException * @throws ORMException */ - public function find($entityName, $id, $lockMode = null, $lockVersion = null) + public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion = null) { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); $className = $class->getClassName(); - if ($lockMode !== null) { + if ($lockMode !== LockMode::NONE) { $this->checkLockRequirements($lockMode, $class); } @@ -431,7 +429,6 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null) $this->lock($entity, $lockMode, $lockVersion); break; - case $lockMode === LockMode::NONE: case $lockMode === LockMode::PESSIMISTIC_READ: case $lockMode === LockMode::PESSIMISTIC_WRITE: $persister = $unitOfWork->getEntityPersister($className); diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index efd99cf24a5..32b03a8a1f3 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -10,6 +10,7 @@ use Doctrine\Common\Collections\Selectable; use Doctrine\Common\Inflector\Inflector; use Doctrine\Common\Persistence\ObjectRepository; +use Doctrine\DBAL\LockMode; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Query\ResultSetMappingBuilder; use Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall; @@ -91,14 +92,12 @@ public function clear() * Finds an entity by its primary key / identifier. * * @param mixed $id The identifier. - * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants - * or NULL if no specific lock mode should be used - * during the search. + * @param int $lockMode One of the \Doctrine\DBAL\LockMode::* constants. * @param int|null $lockVersion The lock version. * * @return object|null The entity instance or NULL if the entity can not be found. */ - public function find($id, $lockMode = null, $lockVersion = null) + public function find($id, $lockMode = LockMode::NONE, $lockVersion = null) { return $this->em->find($this->entityName, $id, $lockMode, $lockVersion); } diff --git a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php index 41fa390a0c7..b8b255619e9 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php @@ -792,7 +792,7 @@ public function load( $entity = null, ?AssociationMetadata $association = null, array $hints = [], - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, array $orderBy = [] ) { @@ -905,7 +905,7 @@ public function loadToOneEntity(ToOneAssociationMetadata $association, $sourceEn /** * {@inheritdoc} */ - public function refresh(array $id, $entity, $lockMode = null) + public function refresh(array $id, $entity, $lockMode = LockMode::NONE) { $sql = $this->getSelectSQL($id, null, $lockMode); [$params, $types] = $this->expandParameters($id); @@ -1151,7 +1151,7 @@ private function getManyToManyStatement( public function getSelectSQL( $criteria, ?AssociationMetadata $association = null, - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, $offset = null, array $orderBy = [] @@ -2179,7 +2179,7 @@ public function exists($entity, ?Criteria $extraConditions = null) $alias = $this->getSQLTableAlias($this->class->getTableName()); $sql = 'SELECT 1 ' - . $this->getLockTablesSql(null) + . $this->getLockTablesSql(LockMode::NONE) . ' WHERE ' . $this->getSelectConditionSQL($criteria); [$params, $types] = $this->expandParameters($criteria); diff --git a/lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php b/lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php index e2de9789e65..728fd530590 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php @@ -5,6 +5,7 @@ namespace Doctrine\ORM\Persisters\Entity; use Doctrine\Common\Collections\Criteria; +use Doctrine\DBAL\LockMode; use Doctrine\ORM\Mapping\AssociationMetadata; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ManyToManyAssociationMetadata; @@ -63,7 +64,7 @@ public function getColumnValue($entity, string $columnName); * Gets the SELECT SQL to select one or more entities by a set of field criteria. * * @param Criteria|Criteria[] $criteria - * @param int|null $lockMode + * @param int $lockMode * @param int|null $limit * @param int|null $offset * @param mixed[] $orderBy @@ -73,7 +74,7 @@ public function getColumnValue($entity, string $columnName); public function getSelectSQL( $criteria, ?AssociationMetadata $association = null, - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, $offset = null, array $orderBy = [] @@ -186,16 +187,14 @@ public function exists($entity, ?Criteria $extraConditions = null); /** * Refreshes a managed entity. * - * @param mixed[] $id The identifier of the entity as an associative array from + * @param mixed[] $id The identifier of the entity as an associative array from * column or field names to values. - * @param object $entity The entity to refresh. - * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants - * or NULL if no specific lock mode should be used - * for refreshing the managed entity. + * @param object $entity The entity to refresh. + * @param int $lockMode One of the \Doctrine\DBAL\LockMode::* constants. * * @return void */ - public function refresh(array $id, $entity, $lockMode = null); + public function refresh(array $id, $entity, $lockMode = LockMode::NONE); /** * Loads an entity by a list of field criteria. @@ -204,8 +203,7 @@ public function refresh(array $id, $entity, $lockMode = null); * @param object|null $entity The entity to load the data into. If not specified, a new entity is created. * @param AssociationMetadata|null $association The association that connects the entity to load to another entity, if any. * @param mixed[] $hints Hints for entity creation. - * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants or NULL if no specific lock mode - * should be used for loading the entity. + * @param int $lockMode One of the \Doctrine\DBAL\LockMode::* constants. * @param int|null $limit Limit number of results. * @param mixed[] $orderBy Criteria to order by. * @@ -218,7 +216,7 @@ public function load( $entity = null, ?AssociationMetadata $association = null, array $hints = [], - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, array $orderBy = [] ); diff --git a/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php index 6626bbaa2e9..38a82663b89 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php @@ -209,7 +209,7 @@ public function delete($entity) public function getSelectSQL( $criteria, ?AssociationMetadata $association = null, - $lockMode = null, + $lockMode = LockMode::NONE, $limit = null, $offset = null, array $orderBy = [] diff --git a/lib/Doctrine/ORM/Query.php b/lib/Doctrine/ORM/Query.php index 47a5812e335..679c6f6984a 100644 --- a/lib/Doctrine/ORM/Query.php +++ b/lib/Doctrine/ORM/Query.php @@ -695,7 +695,7 @@ public function setHydrationMode($hydrationMode) */ public function setLockMode($lockMode) { - if (in_array($lockMode, [LockMode::NONE, LockMode::PESSIMISTIC_READ, LockMode::PESSIMISTIC_WRITE], true)) { + if (in_array($lockMode, [LockMode::PESSIMISTIC_READ, LockMode::PESSIMISTIC_WRITE], true)) { if (! $this->em->getConnection()->isTransactionActive()) { throw TransactionRequiredException::transactionRequired(); } @@ -709,14 +709,14 @@ public function setLockMode($lockMode) /** * Get the current lock mode for this query. * - * @return int|null The current lock mode of this query or NULL if no specific lock mode is set. + * @return int The current lock mode of this query. */ public function getLockMode() { $lockMode = $this->getHint(self::HINT_LOCK_MODE); if ($lockMode === false) { - return null; + return LockMode::NONE; } return $lockMode; diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 7fab4b5c291..cc78279ee60 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -547,7 +547,7 @@ public function walkSelectStatement(AST\SelectStatement $AST) $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset ?? 0); } - if ($lockMode === null || $lockMode === false || $lockMode === LockMode::NONE) { + if ($lockMode === false || $lockMode === LockMode::NONE) { return $sql; } diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 1a3985fb8c6..e3c452afef2 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1976,7 +1976,6 @@ public function lock($entity, $lockMode, $lockVersion = null) break; - case $lockMode === LockMode::NONE: case $lockMode === LockMode::PESSIMISTIC_READ: case $lockMode === LockMode::PESSIMISTIC_WRITE: if (! $this->em->getConnection()->isTransactionActive()) {