Skip to content

[DoctrineBridge][WIP] Allow validating every class against unique entity constraint #24974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename $entity $object
  • Loading branch information
Mathieu Lechat committed Nov 15, 2017
commit 0b765afd99727cb95717461eaa11bb445444eb33
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function __construct(ManagerRegistry $registry)
}

/**
* @param object $entity
* @param object $object
* @param Constraint $constraint
*
* @throws UnexpectedTypeException
* @throws ConstraintDefinitionException
*/
public function validate($entity, Constraint $constraint)
public function validate($object, Constraint $constraint)
{
if (!$constraint instanceof UniqueEntity) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UniqueEntity');
Expand All @@ -60,11 +60,12 @@ public function validate($entity, Constraint $constraint)
throw new ConstraintDefinitionException('At least one field has to be specified.');
}

if (null === $entity) {
if (null === $object) {
return;
}

$entityClass = $constraint->entityClass ?: get_class($entity);
$objectClass = get_class($object);
$entityClass = $constraint->entityClass ?: $objectClass;

if ($constraint->em) {
$em = $this->registry->getManager($constraint->em);
Expand All @@ -76,7 +77,7 @@ public function validate($entity, Constraint $constraint)
$em = $this->registry->getManagerForClass($entityClass);

if (!$em) {
throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_class($entity)));
throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', $objectClass));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use $entityClass instead of $objectClass

}
}

Expand All @@ -85,16 +86,16 @@ public function validate($entity, Constraint $constraint)
$criteria = array();
$hasNullValue = false;

foreach ($fields as $formField => $entityField) {
foreach ($fields as $objectField => $entityField) {
if (!$class->hasField($entityField) && !$class->hasAssociation($entityField)) {
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $entityField));
}

$field = new \ReflectionProperty($entityClass, is_int($formField) ? $entityField : $formField);
$field = new \ReflectionProperty($objectClass, is_int($objectField) ? $entityField : $objectField);
if (!$field->isPublic()) {
$field->setAccessible(true);
}
$fieldValue = $field->getValue($entity);
$fieldValue = $field->getValue($object);

if (null === $fieldValue) {
$hasNullValue = true;
Expand Down Expand Up @@ -149,11 +150,11 @@ public function validate($entity, Constraint $constraint)
if (1 === count($result)) {
$match = $result instanceof \Iterator ? $result->current() : current($result);

if ($entity === $match) {
if ($object === $match) {
return;
}

if ($entity instanceof EntityDto && $entity->isForEntity($match)) {
if ($object instanceof EntityDto && $object->isForEntity($match)) {
return;
}
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy