Annotation Error : should be catched and feedback should be better #5341

Open
opened 2026-01-22 15:05:04 +01:00 by admin · 1 comment
Owner

Originally created by @kcassam on GitHub (Dec 6, 2016).

I have a repository with these annotations (blame copy-paste) :

* @ORM\Table(name="foo")
* @ORM\Entity(repositoryClass="AppBundle\Product\FooRepository")
* @ORM\Entity()

and a custom function in it (getNextFoo())

When I call $em->getRepository('AppBundle:Foo')->getNextFoo();

I have an error :

BadMethodCallException: Undefined method 'getNextFoo'. The method name must start with either findBy or findOneBy!

The code does not find my repository

I found the bug :

Because there are 2 @ORM\Entity(), the second one overrides the first one.

If I delete the line * @ORM\Entity(), everything works well.

A better error message/feedback could be fired in that case : "You have 2 @ORM\Entity annotations, you should have only one" or "You have 2 @ORM\Entity annotations, and the latter is empty and will override the former

Originally created by @kcassam on GitHub (Dec 6, 2016). I have a repository with these annotations (blame copy-paste) : * @ORM\Table(name="foo") * @ORM\Entity(repositoryClass="AppBundle\Product\FooRepository") * @ORM\Entity() and a custom function in it (`getNextFoo()`) When I call `$em->getRepository('AppBundle:Foo')->getNextFoo();` I have an error : `BadMethodCallException: Undefined method 'getNextFoo'. The method name must start with either findBy or findOneBy!` The code does not find my repository I found the bug : Because there are 2 `@ORM\Entity()`, the second one overrides the first one. If I delete the line `* @ORM\Entity()`, everything works well. A better error message/feedback could be fired in that case : "You have 2 @ORM\Entity annotations, you should have only one" or "You have 2 @ORM\Entity annotations, and the latter is empty and will override the former
admin added the BugMissing Tests labels 2026-01-22 15:05:04 +01:00
Author
Owner

@Ma27 commented on GitHub (Dec 23, 2016):

Well I'm not sure if this causes issues with inheritance. If an entity inherits from another entity which contains a ORM\Entity annotation and no ORM\MappedSuperclass annotation (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance for instance) this might lead to two Entity annotations here (https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php#L71), but should not cause an exception, right?

@Ma27 commented on GitHub (Dec 23, 2016): Well I'm not sure if this causes issues with inheritance. If an entity inherits from another entity which contains a `ORM\Entity` annotation and no `ORM\MappedSuperclass` annotation (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance for instance) this might lead to two `Entity` annotations here (https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php#L71), but should not cause an exception, right?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5341