relationshipRepository = $relationshipRepository; } public function validate($value, Constraint $constraint) { if (!$constraint instanceof RelationshipNoDuplicate) { throw new UnexpectedTypeException($constraint, RelationshipNoDuplicate::class); } if (!$value instanceof Relationship) { throw new UnexpectedValueException($value, Relationship::class); } $fromPerson = $value->getFromPerson(); $toPerson = $value->getToPerson(); $relationships = $this->relationshipRepository->findBy([ 'fromPerson' => [$fromPerson, $toPerson], 'toPerson' => [$fromPerson, $toPerson], ]); foreach ($relationships as $r) { if (spl_object_hash($r) !== spl_object_hash($value) and ( ($r->getFromPerson() === $fromPerson and $r->getToPerson() === $toPerson) || ($r->getFromPerson() === $toPerson and $r->getToPerson() === $fromPerson) ) ) { $this->context->buildViolation($constraint->message) ->addViolation(); } } } }