Skip to content

Commit 94986af

Browse files
authored
Merge pull request #11430 from W0rma/fix-deprecation-layer-orm-exception
Fix deprecation layer of Doctrine\ORM\ORMException
2 parents c973a62 + ad5c8e4 commit 94986af

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

Diff for: src/ORMException.php

+49-45
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,31 @@
55
namespace Doctrine\ORM;
66

77
use Doctrine\Common\Cache\Cache as CacheDriver;
8-
use Doctrine\Persistence\ObjectRepository;
8+
use Doctrine\ORM\Cache\Exception\InvalidResultCacheDriver;
9+
use Doctrine\ORM\Cache\Exception\MetadataCacheNotConfigured;
10+
use Doctrine\ORM\Cache\Exception\MetadataCacheUsesNonPersistentCache;
11+
use Doctrine\ORM\Cache\Exception\QueryCacheNotConfigured;
12+
use Doctrine\ORM\Cache\Exception\QueryCacheUsesNonPersistentCache;
13+
use Doctrine\ORM\Exception\EntityManagerClosed;
14+
use Doctrine\ORM\Exception\InvalidEntityRepository;
15+
use Doctrine\ORM\Exception\InvalidHydrationMode;
16+
use Doctrine\ORM\Exception\MismatchedEventManager;
17+
use Doctrine\ORM\Exception\MissingIdentifierField;
18+
use Doctrine\ORM\Exception\MissingMappingDriverImplementation;
19+
use Doctrine\ORM\Exception\NamedNativeQueryNotFound;
20+
use Doctrine\ORM\Exception\NamedQueryNotFound;
21+
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
22+
use Doctrine\ORM\Exception\UnexpectedAssociationValue;
23+
use Doctrine\ORM\Exception\UnknownEntityNamespace;
24+
use Doctrine\ORM\Exception\UnrecognizedIdentifierFields;
25+
use Doctrine\ORM\Persisters\Exception\CantUseInOperatorOnCompositeKeys;
26+
use Doctrine\ORM\Persisters\Exception\InvalidOrientation;
27+
use Doctrine\ORM\Persisters\Exception\UnrecognizedField;
28+
use Doctrine\ORM\Repository\Exception\InvalidFindByCall;
29+
use Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall;
30+
use Doctrine\ORM\Tools\Exception\NotSupported;
931
use Exception;
1032

11-
use function get_debug_type;
12-
use function implode;
1333
use function sprintf;
1434

1535
/**
@@ -26,8 +46,7 @@ class ORMException extends Exception
2646
*/
2747
public static function missingMappingDriverImpl()
2848
{
29-
return new self("It's a requirement to specify a Metadata Driver and pass it " .
30-
'to Doctrine\\ORM\\Configuration::setMetadataDriverImpl().');
49+
return MissingMappingDriverImplementation::create();
3150
}
3251

3352
/**
@@ -39,19 +58,19 @@ public static function missingMappingDriverImpl()
3958
*/
4059
public static function namedQueryNotFound($queryName)
4160
{
42-
return new self('Could not find a named query by the name "' . $queryName . '"');
61+
return NamedQueryNotFound::fromName($queryName);
4362
}
4463

4564
/**
46-
* @deprecated Use Doctrine\ORM\Exception\NamedQueryNotFound
65+
* @deprecated Use Doctrine\ORM\Exception\NamedNativeQueryNotFound
4766
*
4867
* @param string $nativeQueryName
4968
*
5069
* @return ORMException
5170
*/
5271
public static function namedNativeQueryNotFound($nativeQueryName)
5372
{
54-
return new self('Could not find a named native query by the name "' . $nativeQueryName . '"');
73+
return NamedNativeQueryNotFound::fromName($nativeQueryName);
5574
}
5675

5776
/**
@@ -63,7 +82,7 @@ public static function namedNativeQueryNotFound($nativeQueryName)
6382
*/
6483
public static function unrecognizedField($field)
6584
{
66-
return new self(sprintf('Unrecognized field: %s', $field));
85+
return new UnrecognizedField(sprintf('Unrecognized field: %s', $field));
6786
}
6887

6988
/**
@@ -78,7 +97,7 @@ public static function unrecognizedField($field)
7897
*/
7998
public static function unexpectedAssociationValue($class, $association, $given, $expected)
8099
{
81-
return new self(sprintf('Found entity of type %s on association %s#%s, but expecting %s', $given, $class, $association, $expected));
100+
return UnexpectedAssociationValue::create($class, $association, $given, $expected);
82101
}
83102

84103
/**
@@ -91,7 +110,7 @@ public static function unexpectedAssociationValue($class, $association, $given,
91110
*/
92111
public static function invalidOrientation($className, $field)
93112
{
94-
return new self('Invalid order by orientation specified for ' . $className . '#' . $field);
113+
return InvalidOrientation::fromClassNameAndField($className, $field);
95114
}
96115

97116
/**
@@ -101,7 +120,7 @@ public static function invalidOrientation($className, $field)
101120
*/
102121
public static function entityManagerClosed()
103122
{
104-
return new self('The EntityManager is closed.');
123+
return EntityManagerClosed::create();
105124
}
106125

107126
/**
@@ -113,7 +132,7 @@ public static function entityManagerClosed()
113132
*/
114133
public static function invalidHydrationMode($mode)
115134
{
116-
return new self(sprintf("'%s' is an invalid hydration mode.", $mode));
135+
return InvalidHydrationMode::fromMode($mode);
117136
}
118137

119138
/**
@@ -123,7 +142,7 @@ public static function invalidHydrationMode($mode)
123142
*/
124143
public static function mismatchedEventManager()
125144
{
126-
return new self('Cannot use different EventManager instances for EntityManager and Connection.');
145+
return MismatchedEventManager::create();
127146
}
128147

129148
/**
@@ -135,11 +154,11 @@ public static function mismatchedEventManager()
135154
*/
136155
public static function findByRequiresParameter($methodName)
137156
{
138-
return new self("You need to pass a parameter to '" . $methodName . "'");
157+
return InvalidMagicMethodCall::onMissingParameter($methodName);
139158
}
140159

141160
/**
142-
* @deprecated Doctrine\ORM\Repository\Exception\InvalidFindByCall
161+
* @deprecated Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall::becauseFieldNotFoundIn()
143162
*
144163
* @param string $entityName
145164
* @param string $fieldName
@@ -149,10 +168,7 @@ public static function findByRequiresParameter($methodName)
149168
*/
150169
public static function invalidMagicCall($entityName, $fieldName, $method)
151170
{
152-
return new self(
153-
"Entity '" . $entityName . "' has no field '" . $fieldName . "'. " .
154-
"You can therefore not call '" . $method . "' on the entities' repository"
155-
);
171+
return InvalidMagicMethodCall::becauseFieldNotFoundIn($entityName, $fieldName, $method);
156172
}
157173

158174
/**
@@ -165,10 +181,7 @@ public static function invalidMagicCall($entityName, $fieldName, $method)
165181
*/
166182
public static function invalidFindByInverseAssociation($entityName, $associationFieldName)
167183
{
168-
return new self(
169-
"You cannot search for the association field '" . $entityName . '#' . $associationFieldName . "', " .
170-
'because it is the inverse side of an association. Find methods only work on owning side associations.'
171-
);
184+
return InvalidFindByCall::fromInverseSideUsage($entityName, $associationFieldName);
172185
}
173186

174187
/**
@@ -178,7 +191,7 @@ public static function invalidFindByInverseAssociation($entityName, $association
178191
*/
179192
public static function invalidResultCacheDriver()
180193
{
181-
return new self('Invalid result cache driver; it must implement Doctrine\\Common\\Cache\\Cache.');
194+
return InvalidResultCacheDriver::create();
182195
}
183196

184197
/**
@@ -188,7 +201,7 @@ public static function invalidResultCacheDriver()
188201
*/
189202
public static function notSupported()
190203
{
191-
return new self('This behaviour is (currently) not supported by Doctrine 2');
204+
return NotSupported::create();
192205
}
193206

194207
/**
@@ -198,7 +211,7 @@ public static function notSupported()
198211
*/
199212
public static function queryCacheNotConfigured()
200213
{
201-
return new self('Query Cache is not configured.');
214+
return QueryCacheNotConfigured::create();
202215
}
203216

204217
/**
@@ -208,7 +221,7 @@ public static function queryCacheNotConfigured()
208221
*/
209222
public static function metadataCacheNotConfigured()
210223
{
211-
return new self('Class Metadata Cache is not configured.');
224+
return MetadataCacheNotConfigured::create();
212225
}
213226

214227
/**
@@ -218,7 +231,7 @@ public static function metadataCacheNotConfigured()
218231
*/
219232
public static function queryCacheUsesNonPersistentCache(CacheDriver $cache)
220233
{
221-
return new self('Query Cache uses a non-persistent cache driver, ' . get_debug_type($cache) . '.');
234+
return QueryCacheUsesNonPersistentCache::fromDriver($cache);
222235
}
223236

224237
/**
@@ -228,7 +241,7 @@ public static function queryCacheUsesNonPersistentCache(CacheDriver $cache)
228241
*/
229242
public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache)
230243
{
231-
return new self('Metadata Cache uses a non-persistent cache driver, ' . get_debug_type($cache) . '.');
244+
return MetadataCacheUsesNonPersistentCache::fromDriver($cache);
232245
}
233246

234247
/**
@@ -238,7 +251,7 @@ public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache)
238251
*/
239252
public static function proxyClassesAlwaysRegenerating()
240253
{
241-
return new self('Proxy Classes are always regenerating.');
254+
return ProxyClassesAlwaysRegenerating::create();
242255
}
243256

244257
/**
@@ -250,9 +263,7 @@ public static function proxyClassesAlwaysRegenerating()
250263
*/
251264
public static function unknownEntityNamespace($entityNamespaceAlias)
252265
{
253-
return new self(
254-
sprintf("Unknown Entity namespace alias '%s'.", $entityNamespaceAlias)
255-
);
266+
return UnknownEntityNamespace::fromNamespaceAlias($entityNamespaceAlias);
256267
}
257268

258269
/**
@@ -264,11 +275,7 @@ public static function unknownEntityNamespace($entityNamespaceAlias)
264275
*/
265276
public static function invalidEntityRepository($className)
266277
{
267-
return new self(sprintf(
268-
"Invalid repository class '%s'. It must be a %s.",
269-
$className,
270-
ObjectRepository::class
271-
));
278+
return InvalidEntityRepository::fromClassName($className);
272279
}
273280

274281
/**
@@ -281,7 +288,7 @@ public static function invalidEntityRepository($className)
281288
*/
282289
public static function missingIdentifierField($className, $fieldName)
283290
{
284-
return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className));
291+
return MissingIdentifierField::fromFieldAndClass($fieldName, $className);
285292
}
286293

287294
/**
@@ -294,10 +301,7 @@ public static function missingIdentifierField($className, $fieldName)
294301
*/
295302
public static function unrecognizedIdentifierFields($className, $fieldNames)
296303
{
297-
return new self(
298-
"Unrecognized identifier fields: '" . implode("', '", $fieldNames) . "' " .
299-
"are not present on class '" . $className . "'."
300-
);
304+
return UnrecognizedIdentifierFields::fromClassAndFieldNames($className, $fieldNames);
301305
}
302306

303307
/**
@@ -307,6 +311,6 @@ public static function unrecognizedIdentifierFields($className, $fieldNames)
307311
*/
308312
public static function cantUseInOperatorOnCompositeKeys()
309313
{
310-
return new self("Can't use IN operator on entities that have composite keys.");
314+
return CantUseInOperatorOnCompositeKeys::create();
311315
}
312316
}

Diff for: tests/Tests/ORM/Functional/Ticket/GH6123Test.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Doctrine\DBAL\Types\Types;
88
use Doctrine\ORM\Mapping as ORM;
9-
use Doctrine\ORM\Mapping\Column;
109
use Doctrine\ORM\UnitOfWork;
1110
use Doctrine\Tests\OrmFunctionalTestCase;
1211

@@ -17,7 +16,7 @@ protected function setUp(): void
1716
parent::setUp();
1817

1918
$this->createSchemaForModels(
20-
GH6123Entity::class,
19+
GH6123Entity::class
2120
);
2221
}
2322

@@ -58,7 +57,7 @@ public function testRemovedEntityCanBePersistedAgain(): void
5857
$this->_em->flush();
5958
}
6059

61-
private function loadEntityFromDatabase(int $id): GH6123Entity|null
60+
private function loadEntityFromDatabase(int $id): ?GH6123Entity
6261
{
6362
return $this->_em->createQueryBuilder()
6463
->select('e')
@@ -70,11 +69,20 @@ private function loadEntityFromDatabase(int $id): GH6123Entity|null
7069
}
7170
}
7271

72+
/**
73+
* @ORM\Entity
74+
*/
7375
#[ORM\Entity]
7476
class GH6123Entity
7577
{
78+
/**
79+
* @ORM\Id
80+
* @ORM\GeneratedValue
81+
* @ORM\Column(type="integer")
82+
* @var int
83+
*/
7684
#[ORM\Id]
7785
#[ORM\GeneratedValue]
78-
#[Column(type: Types::INTEGER, nullable: false)]
79-
public int $id;
86+
#[ORM\Column(type: Types::INTEGER)]
87+
public $id;
8088
}

0 commit comments

Comments
 (0)