2013-02-28 43 views
0

我正在使用Symfony2。我有两个类:employeephysiciandoctoremployee的子类。symfony2断言继承

实体employee

@UniqueEntity(fields={"email"}, message="Este valor ya se ha utilizado.") 

当我uniqueentity有效的电子邮件字段只针对单独的员工和医生验证。

如果我插入employee3 [email protected]错误断言Symfony2的工作OK
如果我插入doctor2 [email protected]错误断言Symfony2的工作OK
如果我插入doctor2 [email protected]显示错误的SQL,但没有错误断言Symfony2的。

回答

0

这里是UniqueEntityValidator类的一部分:

$repository = $em->getRepository($className); 
$result = $repository->{$constraint->repositoryMethod}($criteria); 

/* If the result is a MongoCursor, it must be advanced to the first 
* element. Rewinding should have no ill effect if $result is another 
* iterator implementation. 
*/ 
if ($result instanceof \Iterator) { 
    $result->rewind(); 
} 

/* If no entity matched the query criteria or a single entity matched, 
* which is the same as the entity being validated, the criteria is 
* unique. 
*/ 
if (0 === count($result) || (1 === count($result) && $entity === 
    ($result instanceof \Iterator ? $result->current() : current($result)))) { 
    return; 
} 

正如你所看到的,$em->getRepository($className)将被用作仓库。问题是,没有repositoryMethod选项,Symfony将使用Doctrine的findBy方法,在每个存储库中都可用,甚至可以从EntityRepository继承自定义的方法。

当你的实体是Employee,symfony会寻找与员工之间的给定域的现有的实体只有

$em->getRepository('AcmeHelloBundle:Employee')->findBy($criteria); 

同样的情况发生在实体Physician不仅看起来医生中):

$em->getRepository('AcmeHelloBundle:Physician')->findBy($criteria); 

引用您:

如果我插入employee3 [email protected]错误断言Symfony2的工作OK

这是因为没有与电子邮件没有员工[email protected]

如果我插入doctor2 mail3 @邮件.com error assert symfony2 work OK

出于与上述相同的原因。

如果我插入doctor2 [email protected]显示错误的SQL,但没有错误断言 Symfony2的

这是问题所在!没有医生与[email protected]电子邮件!有一名员工使用同一封电子邮件,但是在使用findBy针对医师存储库时,它将返回0

也许你应该设置repositoryMethod选项到资源库方法使用数组作为标准搜索时将寻找employeephysician