/** * @param ExecutionContextInterface $context * @param AttributeInterface $attribute * @param DataInterface $data * * @throws Exception */ protected function checkUnique(ExecutionContextInterface $context, AttributeInterface $attribute, DataInterface $data) { $valueData = $data->get($attribute->getCode()); if ($attribute->getType() instanceof IdentifierAttributeType) { /** @var DataRepository $repo */ $repo = $this->doctrine->getRepository($data->getFamily()->getDataClass()); $result = $repo->findByIdentifier($data->getFamily(), $valueData); if ($result && $result->getId() !== $data->getId()) { $this->buildAttributeViolation($context, $attribute, 'unique', $valueData); } return; } /** @var ValueRepository $repo */ $repo = $this->doctrine->getRepository($data->getFamily()->getValueClass()); $values = $repo->findBy(['attributeCode' => $attribute->getCode(), $attribute->getType()->getDatabaseType() => $valueData]); /** @var ValueInterface $value */ foreach ($values as $value) { if (!$value->getData()) { continue; // @warning this should not occur ! Log an error } if ($value->getData()->getId() !== $data->getId()) { $this->buildAttributeViolation($context, $attribute, 'unique', $valueData); return; } } }