Unique Limitations in Doctrine 2, Symfony 2

I want to create a unique constraint in my Doctrine 2 entity so that name and test are a unique column. Value

  • obj1

    • name: name1
    • test: test
  • obj2

    • name: name2
    • test: test <---- duplicated

This should result in an error when retesting.

I tried using a unique constraint ( Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity ). I tried

  * @UniqueEntity("name") * @UniqueEntity("test") 

and

  * @UniqueEntity({"name", "test"}) 

Both seem to only throw an error when I have the name BOTH and the check is duplicated. eg.

  • obj1

    • name: name1
    • test: test
  • obj2

    • name: name2
    • test: test

What is the correct setting? Or could I make a mistake somewhere?

Perhaps I should include the annotation of the doctrine, for example:

 @Table(name="ecommerce_products",uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "email"})}) 

But what, in my opinion, does not cope with my validation of the symfony form?

UPDATE

My test code is:

 /** * @ORM\Entity * @ORM\Table(name="roles") * @UniqueEntity("name") * @UniqueEntity("test") */ class Role { /** * @var integer * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue */ protected $id; /** * @var string * * @ORM\Column(type="string", length=32, unique=true) * @Assert\MaxLength(32) * @Assert\Regex("/^[a-zA-Z0-9_]+$/") */ protected $name; } $v = $this->get('validator'); $role = new Role(); $role->setName('jm'); $role->setTest('test'); $e = $v->validate($role); echo '=== 1 ==='; var_dump($e); if (count($e) == 0) $em->persist($role); $role2 = new Role(); $role2->setName('john'); $role2->setTest('test'); $e = $v->validate($role2); echo '=== 2 ==='; var_dump($e); if (count($e) == 0) $em->persist($role2); $em->flush(); 

In the first run (empty table):

 === 1 ===object(Symfony\Component\Validator\ConstraintViolationList)#322 (1) { ["violations":protected]=> array(0) { } } === 2 ===object(Symfony\Component\Validator\ConstraintViolationList)#289 (1) { ["violations":protected]=> array(0) { } } 

But I get a database-level error message about a unique constraint. So how should I get a verification level that works?

+23
source share
2 answers

They check the fields separately:

 @UniqueEntity("name") @UniqueEntity("test") 

That is, the first one will be launched when there is a duplicate value of name , and the second - if there are duplicates of test .

If you want the test to fail if both name and test contain the same combination, you use this:

 @UniqueEntity({"name", "test"}) 

Why do you want the first approach to work - if you haven’t done something wrong elsewhere. Also try clearing the cache to make sure that this is not his fault.

UPDATE

What I suggested concerned the part of the application-side validation. If you are creating a database schema using Doctrine, you need to provide Doctrine level annotations for each column - if you want to make them unique independently, of course:

 @Column(type = "string", unique = true) private $name; @Column(type = "string", unique = true) private $test; 

These approaches complement each other - do not exclude. @UniqueEntity ensures that the duplicate does not reach the database level, and @Column ensures that if it does, the database level will not let it pass.

+49
source

In the table annotation, you can also set the index for multiple columns .

  / **
  * @ORM \ Entity
  * @ORM \ Table (name = "ecommerce_products", uniqueConstraints = {
  * @ORM \ UniqueConstraint (name = "search_idx", columns = {"name", "email"})})
  * /

or with YAML format:

 Namespace\Entity\EntityName: type: entity table: ecommerce_products uniqueConstraints: uniqueConstraint: columns: [name, email] 
+48
source

All Articles