Symfony2 UniqueEntity multiple fields: false positive check?

I am trying to verify the uniqueness of an object submitted from a form using the uniqueness check constraint in several fields.

The object code, which must be unique, has two fields: fieldA and fieldB , both unique:

/**
 * @ORM\Table(name="mytable")
 * @ORM\Entity
 * @DoctrineAssert\UniqueEntity(fields = {"fieldA", "fieldB"})
 */
class myClass
{
  /**
   * @ORM\Column(name="fieldA", type="string", length=128, unique=true)
   */
  protected $fieldA;

  /**
   * @ORM\Column(name="fieldB", type="string", length=128, unique=true)
   */
  protected $fieldB;
}

Suppose I already have an entry in the database with the values:

  • fieldA = 'value_a', fieldB = 'value_b'

Now, when I try to submit another form with values ​​(fieldA = 'value_a', fieldB = 'value_c') from the form, Symfony2 generates a request to verify uniqueness:

SELECT ... FROM ... WHERE fieldA = ? AND fieldB = ? ('value_a', 'value_c')

, , , , fieldA . ( SQL "value_a".)

Symfony2 UniqueEntity :

- ( ), . , , .

, .

UniqueEntityValidator ( 94), "findBy" . "" , .

- , ?

+5
2

:

/**
 * @ORM\Table(name="mytable")
 * @ORM\Entity
 * @DoctrineAssert\UniqueEntity(fields = "fieldA")
 * @DoctrineAssert\UniqueEntity(fields = "fieldB")
 */
class myClass

?

+8

:

/**
 * @ORM\Table(name="mytable")
 * @ORM\Entity
 * @DoctrineAssert\UniqueEntity(fields = "fieldA")
 * @DoctrineAssert\UniqueEntity(fields = "fieldB")
 */
class myClass

 * @DoctrineAssert\UniqueEntity(fields = {"fieldA", "fieldB"})

, - .

, , :

fieldA = 'value_a', fieldB = 'value_b'

, (fieldA = 'value_a', fieldB = 'value_c') , Symfony2 :

SELECT... FROM... WHERE fieldA =? AND fieldB =? ('value_a', 'value_c')

,

fieldA = 'value_a', fieldB = 'value_b'

, yo (fieldA = 'value_a', fieldB = 'value_b') , .

: http://symfony.com/doc/current/reference/constraints/UniqueEntity.html#fields

+13

All Articles