教义中的独特约束2,Symfony 2 [英] Unique Constraints in Doctrine 2, Symfony 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)
如果您希望 对于你想要的第一种方法应该工作 - 除非你在别的地方做错了事情。还要尝试清除缓存,以确保它不是它的错误。 更新 我建议的是关于应用程序端的验证部分。如果您使用Doctrine生成数据库模式,那么您需要为每个列提供Doctrine级别注释 - 如果要使它们彼此独立,那么当然: 这些方法互补 - 不排除。 I want to make a unique constraint in my Doctrine 2 entity such that obj1 obj2 This should trigger an error as test is duplicated. I tried using the unique constraint ( and Both seem to only trigger error when I have BOTH name and test duplicated. eg. obj1 obj2 Whats the right setup? Or I might have made a mistake somewhere? Perhaps I should include the doctrine annotation like: But that still wont handle my symfony form validation I think? UPDATE My test code: On first run (empty table): 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: That is, the first one will get triggered when there is a duplicate If you want the validation fail when both 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: These approaches complement each other — not exclude. 这篇关于教义中的独特约束2,Symfony 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
也就是说,当有重复的名称
值时,第一个将被触发,而第二个 - 当有重复的测试时$>
name
和 test
包含相同的组合,您可以使用以下内容:
@UniqueEntity({name,test})
@Column(type =string,unique = true)
private $ name;
@Column(type =string,unique = true)
private $ test;
@UniqueEntity
确保重复甚至不能达到数据库层,而 @Column
确保如果它是,数据库层不会让它通过。name
& test
are unique column wise. Meaning
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
). Tried * @UniqueEntity("name")
* @UniqueEntity("test")
* @UniqueEntity({"name", "test"})
@Table(name="ecommerce_products",uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "email"})})
/**
* @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();
=== 1 ===object(Symfony\Component\Validator\ConstraintViolationList)#322 (1) {
["violations":protected]=>
array(0) {
}
}
=== 2 ===object(Symfony\Component\Validator\ConstraintViolationList)#289 (1) {
["violations":protected]=>
array(0) {
}
}
@UniqueEntity("name")
@UniqueEntity("test")
name
value, while the second one — when there is a duplicate test
values.name
and test
contain the same combination, you use this:@UniqueEntity({"name", "test"})
@Column(type = "string", unique = true)
private $name;
@Column(type = "string", unique = true)
private $test;
@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.