-
-
Notifications
You must be signed in to change notification settings - Fork 375
[LiveComponent] Add support for interfaces while (de)hydrating a Doctrine entity #2834
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
base: 2.x
Are you sure you want to change the base?
Conversation
…iased to an entity Doctrine have the feature to use interface aliased to a concrete entity class. This PR propose a small modification to ensure that is will also supported by the doctrine hydrator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your first contribution :)
src/LiveComponent/src/Hydration/DoctrineEntityHydrationExtension.php
Outdated
Show resolved
Hide resolved
src/LiveComponent/src/Hydration/DoctrineEntityHydrationExtension.php
Outdated
Show resolved
Hide resolved
src/LiveComponent/src/Hydration/DoctrineEntityHydrationExtension.php
Outdated
Show resolved
Hide resolved
Making tests
The last commit provides some tests. |
'resolve_target_entities' => [ | ||
AliasedEntityInterface::class => AliasedEntity::class], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'resolve_target_entities' => [ | |
AliasedEntityInterface::class => AliasedEntity::class], | |
'resolve_target_entities' => [ | |
AliasedEntityInterface::class => AliasedEntity::class, | |
], |
@@ -50,4 +53,34 @@ public function testForeignKeyId(): void | |||
self::assertSame($foreignKeyIdEntity->id->id, $dehydrated); | |||
self::assertSame($foreignKeyIdEntity, $extension->hydrate($dehydrated, ForeignKeyIdEntity::class)); | |||
} | |||
|
|||
public function testSupportInterface(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testSupportInterface(): void | |
public function testSupportInterface() |
self::assertFalse($extension->supports('UnknownClass'), 'UnknownClass should not be supported'); | ||
} | ||
|
||
public function testHydrationFromInterface(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testHydrationFromInterface(): void | |
public function testHydrationFromInterface() |
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Entity; | ||
|
||
interface AliasedEntityInterface { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
class Aliased | ||
{ | ||
// public string $address; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// public string $address; |
// Keep the standard way for a class | ||
// todo cache/warmup an array of classes that are "doctrine objects" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Keep the standard way for a class | |
// todo cache/warmup an array of classes that are "doctrine objects" |
foreach ($this->managerRegistries as $registry) { | ||
foreach ($registry->getManagers() as $om) { | ||
// the getClassMetaData can indeed throw an exception | ||
// so, we |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
} catch (MappingException $e) { | ||
// I did not find a nice way to check if it is because the class is really unknown | ||
// It is good to check for a specific exception ? | ||
// eg: \Doctrine\Persistence\Mapping\MappingException | ||
// @see \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getMetadataFor | ||
// throw $e; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We 100% can catch a specific exception if that allows another object manager to be selected during following iterations of the loop(s).
// Fore more details : | ||
// @see \Doctrine\ORM\Tools\ResolveTargetEntityListener | ||
|
||
// todo cache/warmup an array of interfaces that are resolved "doctrine objects" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// todo cache/warmup an array of interfaces that are resolved "doctrine objects" |
// special handler for interfaces | ||
// For use cases : @see https://symfony.com/doc/current/doctrine/resolve_target_entity.html | ||
// As today, getManagerForClass don't resolve nicely aliased interfaces | ||
// The workaround is to enum over each object manager and trying to get metadata | ||
// The metadata are indeed, resolved nicely | ||
// Fore more details : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we turn this into two or three lines, insisting more on what we "do" than on what "was" the problem ?
Doctrine have the feature to use interface aliased to a concrete entity class.
This PR propose a small modification to ensure that is will also supported by the doctrine hydrator