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 がfindBy
Doctrine によるメソッドを使用することです。これは、すべてのリポジトリで利用可能であり、EntityRepository
.
エンティティが の場合、Symfony は従業員のみから指定されたフィールドを持つ既存のエンティティをEmployee
探します:
$em->getRepository('AcmeHelloBundle:Employee')->findBy($criteria);
エンティティがPhysician
(医師の間でのみ見える) 場合も同じことが起こります。
$em->getRepository('AcmeHelloBundle:Physician')->findBy($criteria);
あなたの引用:
employee3 を挿入すると、mail1@mail.com エラーが symfony2 の動作 OK をアサートします
これは、メール mail1@mail.com を持つ従業員がいないためです。
doctor2 mail3@mail.com エラーを挿入すると、symfony2 の動作 OK がアサートされます
上記と同じ理由で。
doctor2 mail1@mail.com を挿入すると、エラー sql が表示されますが、symfony2 をアサートするエラーはありません
これが問題です!mail1@mail.com メールで医者はいません!同じメールの従業員がいますが、findBy
医師のリポジトリに対して使用すると、 が返されます0
。
おそらく、配列を基準として使用して検索する場合、またはrepositoryMethod
検索するリポジトリ メソッドにオプションを設定する必要があります。employee
physician