I want to create a unique constraint in my Doctrine 2 entity so that name and test are a unique column. Value
obj1
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.
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:
class Role { protected $id; 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?