Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

validation - Unique Constraints in Doctrine 2, Symfony 2

I want to make a unique constraint in my Doctrine 2 entity such that name & test are unique column wise. Meaning

  • obj1

    • name: name1
    • test: test
  • obj2

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

This should trigger an error as test is duplicated.

I tried using the unique constraint (SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity). Tried

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

and

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

Both seem to only trigger error when I have BOTH name and test duplicated. eg.

  • obj1

    • name: name1
    • test: test
  • obj2

    • name: name2
    • test: test

Whats the right setup? Or I might have made a mistake somewhere?

Perhaps I should include the doctrine annotation like:

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

But that still wont handle my symfony form validation I think?

UPDATE

My test code:

/**
 * @ORMEntity
 * @ORMTable(name="roles") 
 * @UniqueEntity("name")
 * @UniqueEntity("test")
 */
class Role {

    /**
     * @var integer
     * @ORMColumn(type="integer")
     * @ORMId
     * @ORMGeneratedValue
     */
    protected $id;

    /**
     * @var string
     * 
     * @ORMColumn(type="string", length=32, unique=true)
     * @AssertMaxLength(32)
     * @AssertRegex("/^[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();

On first run (empty table):

=== 1 ===object(SymfonyComponentValidatorConstraintViolationList)#322 (1) {
  ["violations":protected]=>
  array(0) {
  }
}
=== 2 ===object(SymfonyComponentValidatorConstraintViolationList)#289 (1) {
  ["violations":protected]=>
  array(0) {
  }
}

But I do get an error on database layer about unique constraint. So how should I get Validation layer working tho?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

/**
 * @ORMEntity
 * @ORMTable(name="ecommerce_products",uniqueConstraints={
 *     @ORMUniqueConstraint(name="search_idx", columns={"name", "email"})})
 */

or with YAML format:

NamespaceEntityEntityName:
    type: entity
    table: ecommerce_products
    uniqueConstraints:
        uniqueConstraint:
            columns: [name, email]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...