开发者

Symfony2 validation exception with unique constraint

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;

  ..


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.


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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜