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

UniqueEntityValidator in SingleTableInheritance #17962

Closed
Nemo64 opened this Issue Feb 29, 2016 · 8 comments

Comments

Projects
None yet
7 participants
@Nemo64

Nemo64 commented Feb 29, 2016

I have multiple kinds of entities within a single table inheritance. The UniqueEntityValidator fails in that scenario as it uses get_class($entity). It instead should use the class on which it is defined.

Example:

/**
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string", length=15)
 * @UniqueEntity(fields={"email"})
 */
class User {
    /** @ORM\Column(type="string") */
    private $email;
}
class Collaborator extends User {}

Validating the Collaborator will now only check for other Collaborators with the same email.

I'm using symfony 2.7.10.

@iltar

This comment has been minimized.

Contributor

iltar commented Mar 1, 2016

I think you're correct, the validator uses the following checks:

  • $class = $em->getClassMetadata(get_class($entity));
  • $repository = $em->getRepository(get_class($entity));

Do you happen to have a different repository defined? You could try to see if the repositoryMethod in the constraint will fix this for you.

@Nemo64

This comment has been minimized.

Nemo64 commented Mar 1, 2016

@iltar Why should it? It would then call the repository method on the CollaboratorRepository. I could probably create a method there which would also return normal Users but that wouldn't be really nice would it? ;)

@iltar

This comment has been minimized.

Contributor

iltar commented Mar 1, 2016

@Nemo64 I'm just proposing possible solutions to get your code running. If your entities share the same repository, it shouldn't matter. If they have a different repository, you might want to define the one of the parent entity/class to be the correct one.

@Nemo64

This comment has been minimized.

Nemo64 commented Mar 1, 2016

@iltar I know and thanks for that. I hope I don't sound harsh, that wasn't my intention.

But I have the feeling i'm missing something. The UniqueEntityValidator uses ->getRepository(get_class($entity)). Therefor doctrine will create an instance of EntityRepository which is configured to give me instances of that class. I can overwrite that class to so doctrine creates a UserRepository but that won't change the configuration done by doctrines repository factory. The validator will use ->getRepository("Collaborator") and get a Repository that will only return Collaborators unless i create a method that'll ignore the configuration of that repository.

@xabbuh xabbuh added the Validator label Mar 1, 2016

@craue

This comment has been minimized.

Contributor

craue commented Mar 3, 2016

Is this considered to be a bug and will be fixed or do I have to add a custom validator instead?

@Nemo64, in your code example, @UniqueEntityValidator should be @UniqueEntity instead.

@Nemo64

This comment has been minimized.

Nemo64 commented Mar 3, 2016

@craue you are right, didn't notice. I updated the example. thanks.

@ogizanagi

This comment has been minimized.

Member

ogizanagi commented Mar 6, 2016

Related to #15002 (+issues) and #16969 (+PRs: #16978 & #16981)

@ogizanagi

This comment has been minimized.

Member

ogizanagi commented Oct 14, 2016

#15002 is merged and allows to select and fix the repository to use. See symfony/symfony-docs#7057 for the documentation (maybe it can be improved thanks to you)

@xabbuh xabbuh closed this Oct 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment