Skip to content

Commit 021164f

Browse files
committed
throw when attempting to use partial clearing
1 parent b2d0c21 commit 021164f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/Doctrine/ORM/EntityRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
use Doctrine\Common\Collections\AbstractLazyCollection;
99
use Doctrine\Common\Collections\Criteria;
1010
use Doctrine\Common\Collections\Selectable;
11+
use Doctrine\Common\Persistence\PersistentObject;
1112
use Doctrine\DBAL\LockMode;
1213
use Doctrine\Deprecations\Deprecation;
1314
use Doctrine\Inflector\Inflector;
1415
use Doctrine\Inflector\InflectorFactory;
16+
use Doctrine\ORM\Exception\NotSupported;
1517
use Doctrine\ORM\Mapping\ClassMetadata;
1618
use Doctrine\ORM\Query\ResultSetMappingBuilder;
1719
use Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall;
1820
use Doctrine\Persistence\ObjectRepository;
1921

2022
use function array_slice;
23+
use function class_exists;
2124
use function lcfirst;
2225
use function sprintf;
2326
use function str_starts_with;
@@ -168,6 +171,13 @@ public function clear()
168171
__METHOD__
169172
);
170173

174+
if (! class_exists(PersistentObject::class)) {
175+
throw NotSupported::createForPersistence3(sprintf(
176+
'Partial clearing of entities for class %s',
177+
$this->_class->rootEntityName
178+
));
179+
}
180+
171181
$this->_em->clear($this->_class->rootEntityName);
172182
}
173183

lib/Doctrine/ORM/Exception/NotSupported.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\ORM\Exception;
66

7+
use function sprintf;
8+
79
final class NotSupported extends ORMException
810
{
911
public static function create(): self
@@ -15,4 +17,16 @@ public static function createForDbal3(): self
1517
{
1618
return new self('Feature was deprecated in doctrine/dbal 2.x and is not supported by installed doctrine/dbal:3.x, please see the doctrine/deprecations logs for new alternative approaches.');
1719
}
20+
21+
public static function createForPersistence3(string $context): self
22+
{
23+
return new self(sprintf(
24+
<<<'EXCEPTION'
25+
Context: %s
26+
Problem: Feature was deprecated in doctrine/persistence 2.x and is not supported by installed doctrine/persistence:3.x
27+
Solution: See the doctrine/deprecations logs for new alternative approaches.
28+
EXCEPTION,
29+
$context
30+
));
31+
}
1832
}

tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use BadMethodCallException;
88
use Doctrine\Common\Collections\ArrayCollection;
99
use Doctrine\Common\Collections\Criteria;
10+
use Doctrine\Common\Persistence\PersistentObject;
1011
use Doctrine\DBAL\Connection;
1112
use Doctrine\DBAL\LockMode;
1213
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;
1314
use Doctrine\DBAL\ParameterType;
1415
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1516
use Doctrine\ORM\EntityRepository;
1617
use Doctrine\ORM\Exception\InvalidEntityRepository;
18+
use Doctrine\ORM\Exception\NotSupported;
1719
use Doctrine\ORM\Exception\ORMException;
1820
use Doctrine\ORM\Exception\UnrecognizedIdentifierFields;
1921
use Doctrine\ORM\Mapping\MappingException;
@@ -1155,7 +1157,12 @@ public function testDeprecatedClear(): void
11551157
{
11561158
$repository = $this->_em->getRepository(CmsAddress::class);
11571159

1158-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8460');
1160+
if (class_exists(PersistentObject::class)) {
1161+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8460');
1162+
} else {
1163+
$this->expectException(NotSupported::class);
1164+
$this->expectExceptionMessage(CmsAddress::class);
1165+
}
11591166

11601167
$repository->clear();
11611168
}

0 commit comments

Comments
 (0)