0

I have an entity "Movie" which has a unique constraint through doctrine annotation. Based on the movie entity I have auto generated a CRUD layer. When I now try to add a new movie I get the following exception:

Only field names mapped by Doctrine can be validated for uniqueness.

When the constraint is removed everything works fine. Do somebody has an idea where the problem lays and how I can resolve it?

My guessing is the entity, because it is new, is not sync with the EntityManager and therefore could not check the constraint. Am I'close?

I'm using Symfony 2.0.1 with Doctrine 2.1.1, MySQL as Database.

Thanks,
-lony

The "Movie" Entity:

/**
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"movie" = "Movie", "series" = "Series"})
 * 
 * @DoctrineAssert\UniqueEntity("title_orginal")
 */
class Movie {

  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @var string $titleOrginal
   *
   * @ORM\Column(name="title_orginal", type="string", length=255, unique="true")
   */
  private $titleOrginal;

  ..
1
  • So? Did you solve your problem? Commented Oct 21, 2011 at 10:27

2 Answers 2

3

Your syntax is wrong. Use this:

@DoctrineAssert\UniqueEntity(fields={"title_orginal"})

instead of

@DoctrineAssert\UniqueEntity("title_orginal")

You can then customize the violation message like this :

@DoctrineAssert\UniqueEntity(fields={"title_orginal", message="my.custom.message"})

and translate this message by using a validators.xliff file (it must be named like that). I tell you this because I struggled the other day with it and was obliged to debug to find about this validators.xliff naming convention.

Sign up to request clarification or add additional context in comments.

2 Comments

Without the @? So if it isn't an annotation what else is it?
With the @, of course. Sorry for the typo. I added your original code to my post so that you can spot the difference more easily.
3

I think there is a small typo:

@DoctrineAssert\UniqueEntity(fields={"title_orginal", message="my.custom.message"})

should be:

@DoctrineAssert\UniqueEntity(fields={"title_orginal"}, message="my.custom.message")

and for several fields

@DoctrineAssert\UniqueEntity(fields={"title_orginal", "field2"}, message="my.custom.message")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.