教义中的独特约束2,Symfony 2 [英] Unique Constraints in Doctrine 2, Symfony 2

查看:97
本文介绍了教义中的独特约束2,Symfony 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Doctrine 2实体中做出一个独特的约束,使得 name & test 是明智的列。含义




  • obj1




    • :name1

    • test:test


  • obj2




    • name:name2

    • test:test< ---- duplicateated




这将在测试重复时触发错误。



我尝试使用唯一约束( Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity )。尝试

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

  @UniqueEntity({name,test})

两者似乎只会触发错误,当我有两个名称和测试重复。例如。




  • obj1




    • name: name1

    • test:test


  • obj2




    • name:name2

    • test:test




什么是正确的设置?或者我可能在某个地方犯了一个错误?



也许我应该包括这样的教义注释,如:

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

但是,我仍然不会处理我的symfony表单验证?



更新



我的测试代码:

  
* @ ORM\Entity
* @ ORM\Table(name =roles)
* @UniqueEntity(name)
* @UniqueEntity(测试)
* /
class角色{

/ **
* @var整数
* @ 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 _] + $ /)
* /
受保护的$ 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();

首次运行(空表):

  === 1 === object(Symfony\Component\Validator\ConstraintViolationList)#322(1){
[violation:protected] => ;
数组(0){
}
}
=== 2 === object(Symfony\Component\Validator\ConstraintViolationList)#289(1){
[violation:protected] =>
数组(0){
}
}

但是在数据库层上获取有关唯一约束的错误。那么我应该如何获得验证层工作?

解决方案

这些字段单独检查:

  @UniqueEntity(name)
@UniqueEntity(test)



也就是说,当有重复的名称值时,第一个将被触发,而第二个 - 当有重复的测试时

如果您希望 name test 包含相同的组合,您可以使用以下内容:

  @UniqueEntity({name,test})

对于你想要的第一种方法应该工作 - 除非你在别的地方做错了事情。还要尝试清除缓存,以确保它不是它的错误。



更新



我建议的是关于应用程序端的验证部分。如果您使用Doctrine生成数据库模式,那么您需要为每个列提供Doctrine级别注释 - 如果要使它们彼此独立,那么当然:

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

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

这些方法互补 - 不排除。 @UniqueEntity 确保重复甚至不能达到数据库层,而 @Column 确保如果它是,数据库层不会让它通过。


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 (Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity). 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:

/**
 * @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();

On 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 do get an error on database layer about unique constraint. So how should I get Validation layer working tho?

解决方案

These check for the fields individually:

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

That is, the first one will get triggered when there is a duplicate name value, while the second one — when there is a duplicate test values.

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

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

For what you want the first approach should work — unless you did something wrong somewhere else. Also try to clear the cache to make sure it's not its fault.

UPDATE

What I suggested was about the validation part on the app side. If you generate the database schema using Doctrine, you'll need to supply the Doctrine level annotations for each column — if you want to make them unique independently of each other, of course:

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

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

These approaches complement each other — not exclude. @UniqueEntity makes sure a duplicate doesn't even reach the database layer, while @Column ensures that if it does, the database layer won't let it pass.

这篇关于教义中的独特约束2,Symfony 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆