0

I'm trying to implement the uniqueEntity functionality to avoid saving twice or more in DB but it seems not working.

I red this : http://symfony.com/doc/current/reference/constraints/UniqueEntity.html

Here is my code :

Am I doing something wrong ?

...

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * Artist
 *
 * @ORM\Table(name="artists")
 * @ORM\Entity
 * @UniqueEntity(fields = {"firstName", "lastName", "birthDate", "deathDate"})
 */
class Artist
{
    /**
     * @var integer
     *
     * @ORM\Column(name="ArtistID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="ArtistFirstName", type="string", length=100, nullable=true)
     * 
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(name="ArtistLastName", type="string", length=100)
     * @Assert\NotBlank()
     */
    private $lastName;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="ArtistBirthDate", type="datetime", nullable=true)
     */
    private $birthDate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="ArtistDeathDate", type="datetime", nullable=true)
     */
    private $deathDate;

...

Any help would be great. Thank you.

Benjamin Lucas
  • 370
  • 5
  • 20
  • Whenever I run into problems like this it's almost always because an older version of my entities are cached in APC/u. What happens if you clear the APC cache? (Restarting Apache will do this.) – nurikabe Mar 21 '15 at 11:58

1 Answers1

0

After a lot of tests, It works !

What I Tried :

  • empty cash
  • delete fields before {} =>@UniqueEntity({"firstName","lastName","birthDate", "deathDate"},)
  • remove unique=true to all of my properties because I want multiple fields and not only one
  • then I put the "fields ... " again and it works

Now I find that the error is not clear for the enduser.

The error always target the first field. Ok, it seems to be logical but is there an other solution to show error ? Maybe on top but so, all errors will be showed there and I prefer to see errors in front of the associate field.

Thank you to all.

Benjamin Lucas
  • 370
  • 5
  • 20