You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to check the behavior of a service, which made use of only parts of an entity. It didn't make sense to build the whole thing as it required a lot of additional setup, and would be a pain to update when the entity in question is expanded (I also consider it a valid use case, as it simplifies unit tests greatly).
Basically, I needed to check that a combination of services:
Acquires an ID and relationship through two methods.
Performs some operations, queries an external service, updates the relationships.
My test checks that relationships were updated correctly, and services were called only when needed.
Whether the test is valid put aside, I was struggling to get the repository for that entity to work. While normal EntityRepository probably works fine (haven't checked), ServiceEntityRepository struggles because FooInterface is not a valid entity.
class FooRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, FooInterface::class);
}
What I ended up doing is adding #[MappedSuperclass] attribute to the interface, so that Doctrine ORM would pick up that this is a valid entity, which only later gets resolved into a proper entity class.
Is this the expected way of doing it? If so, should we update the docs to include this? I have spent some time to figure it out (unless I've done something wrong), so I'd spare others when looking for this feature.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I was following https://www.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/resolve-target-entity-listener.html, because I needed a simplified entity for tests (in a Symfony project).
I wanted to check the behavior of a service, which made use of only parts of an entity. It didn't make sense to build the whole thing as it required a lot of additional setup, and would be a pain to update when the entity in question is expanded (I also consider it a valid use case, as it simplifies unit tests greatly).
Basically, I needed to check that a combination of services:
Whether the test is valid put aside, I was struggling to get the repository for that entity to work. While normal
EntityRepository
probably works fine (haven't checked),ServiceEntityRepository
struggles becauseFooInterface
is not a valid entity.What I ended up doing is adding
#[MappedSuperclass]
attribute to the interface, so that Doctrine ORM would pick up that this is a valid entity, which only later gets resolved into a proper entity class.Is this the expected way of doing it? If so, should we update the docs to include this? I have spent some time to figure it out (unless I've done something wrong), so I'd spare others when looking for this feature.
Beta Was this translation helpful? Give feedback.
All reactions