From 5c607cb13d43c2ad84b244a94702cc4f8adf0fde Mon Sep 17 00:00:00 2001 From: Angelo Date: Sat, 25 Jul 2015 11:30:04 +0200 Subject: [PATCH] Edits to DoctrinePresenseVerifier.php Whatever I did, the current code didn't seem to work. I edited it to make it a DQL query. It does work now. Only changes required for this to work is to include the entity namespace in the doctrine config. --- src/Validation/DoctrinePresenceVerifier.php | 53 ++++++++++++--------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Validation/DoctrinePresenceVerifier.php b/src/Validation/DoctrinePresenceVerifier.php index 820f58d..4da4e8a 100644 --- a/src/Validation/DoctrinePresenceVerifier.php +++ b/src/Validation/DoctrinePresenceVerifier.php @@ -1,6 +1,7 @@ ?'; - } - - foreach ($extra as $key => $extraValue) { - $queryParts[] = "AND $key = ?"; - } - - $query = $this->createQueryFrom($queryParts); - $query->setParameter(1, $value); - - if (!is_null($excludeId) && $excludeId != 'NULL') { - $query->setParameter(2, $excludeId); - } - - foreach ($extra as $key => $extraValue) { - $query->setParameter($key + 3, $extraValue); - } - - return $query->getSingleScalarResult(); + $config = Config::get('doctrine'); + // add the entity namespace to your doctrine config + // i.e. 'entity_namespace' => 'App\\Entity\\', + $namespace = $config['entity_namespace']; + + $query = 'SELECT COUNT(ent) '; + $query .= 'FROM ' . $namespace . $collection . ' ent '; + $query .= 'WHERE ent.' . $column . ' = :value '; + + if (!is_null($excludeId) && $excludeId != 'NULL') { + $query .= 'AND ent.'.($idColumn ?: 'id').' <> :excludeid '; + } + + foreach ($extra as $key => $extraValue) { + $query .= 'AND ent.' . $key . ' = :' . $key . ' '; + } + + $query = $this->entityManager + ->createQuery($query) + ->setParameter('value', $value); + + if (!is_null($excludeId) && $excludeId != 'NULL') { + $query->setParameter('excludeid', $excludeId); + } + + foreach ($extra as $key => $extraValue) { + $query->setParameter($key, $extraValue); + } + + return $query->getSingleScalarResult(); } /**