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

Improve exception message #9646

Merged
merged 1 commit into from
Apr 10, 2022
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
7 changes: 6 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Upgrade to 2.12

## Deprecate omitting `$class` argument to `ORMInvalidArgumentException::invalidIdentifierBindingEntity()`

To make it easier to identify understand the cause for that exception, it is
deprecated to omit the class name when calling
`ORMInvalidArgumentException::invalidIdentifierBindingEntity()`.

## Deprecate `Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper`

Using a console helper to provide the ORM's console commands with one or
multiple entity managers had been deprecated with 2.9 already. This leaves
The `EntityManagerHelper` class with no purpose which is why it is now
deprecated too. Applications that still rely on the `em` console helper, can
easily recreate that class in their own codebase.

## Deprecate custom repository classes that don't extend `EntityRepository`

Although undocumented, it is currently possible to configure a custom repository
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Result;
use Doctrine\Deprecations\Deprecation;
Expand Down Expand Up @@ -434,10 +435,11 @@ public function processParameterValue($value)
}

try {
$class = ClassUtils::getClass($value);
$value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value);

if ($value === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class);
}
} catch (MappingException | ORMMappingException $e) {
/* Silence any mapping exceptions. These can occur if the object in
Expand Down
11 changes: 7 additions & 4 deletions lib/Doctrine/ORM/Cache/DefaultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,14 @@ private function buildCollectionCacheKey(
*/
private function toIdentifierArray(ClassMetadata $metadata, $identifier): array
{
if (is_object($identifier) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($identifier))) {
$identifier = $this->uow->getSingleIdentifierValue($identifier);
if (is_object($identifier)) {
$class = ClassUtils::getClass($identifier);
if ($this->em->getMetadataFactory()->hasMetadataFor($class)) {
$identifier = $this->uow->getSingleIdentifierValue($identifier);

if ($identifier === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
if ($identifier === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class);
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,14 @@ public function find($className, $id, $lockMode = null, $lockVersion = null)
}

foreach ($id as $i => $value) {
if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) {
$id[$i] = $this->unitOfWork->getSingleIdentifierValue($value);

if ($id[$i] === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
if (is_object($value)) {
$className = ClassUtils::getClass($value);
if ($this->metadataFactory->hasMetadataFor($className)) {
$id[$i] = $this->unitOfWork->getSingleIdentifierValue($value);

if ($id[$i] === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($className);
}
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use function array_map;
use function count;
use function func_get_arg;
use function func_num_args;
use function get_debug_type;
use function gettype;
use function implode;
Expand Down Expand Up @@ -198,9 +200,27 @@ public static function invalidCompositeIdentifier()
/**
* @return ORMInvalidArgumentException
*/
public static function invalidIdentifierBindingEntity()
public static function invalidIdentifierBindingEntity(/* string $class */)
{
return new self('Binding entities to query parameters only allowed for entities that have an identifier.');
if (func_num_args() === 0) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9642',
'Omitting the class name in the exception method %s is deprecated.',
__METHOD__
);

return new self('Binding entities to query parameters only allowed for entities that have an identifier.');
}

return new self(sprintf(
<<<'EXCEPTION'
Binding entities to query parameters only allowed for entities that have an identifier.
Class "%s" does not have an identifier.
EXCEPTION
,
func_get_arg(0)
));
}

/**
Expand Down
9 changes: 7 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2084Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ public function testIssue(): void
self::assertEquals('Foo', $e->getMyEntity2()->getValue());
}

public function testinvalidIdentifierBindingEntityException(): void
public function testInvalidIdentifierBindingEntityException(): void
{
$this->expectException('Doctrine\ORM\ORMInvalidArgumentException');
$this->expectExceptionMessage('Binding entities to query parameters only allowed for entities that have an identifier.');
$this->expectExceptionMessage(
<<<'EXCEPTION'
Binding entities to query parameters only allowed for entities that have an identifier.
Class "Doctrine\Tests\ORM\Functional\Ticket\DDC2084\MyEntity2" does not have an identifier.
EXCEPTION
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derrabus isn't this change in this test good enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😓 Sorry.

$this->_em->find(__NAMESPACE__ . '\DDC2084\MyEntity1', new DDC2084\MyEntity2('Foo'));
}
}
Expand Down