Skip to content
Open
Changes from all commits
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
53 changes: 31 additions & 22 deletions src/Validation/DoctrinePresenceVerifier.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mitch\LaravelDoctrine\Validation;

use App;
use EntityManager;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -27,28 +28,36 @@ public function __construct(EntityManagerInterface $entityManager)
*/
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = array())
{
$queryParts = ['SELECT COUNT(*) FROM', $collection, 'WHERE', "$column = ?"];

if (!is_null($excludeId) && $excludeId != 'NULL') {
$queryParts[] = 'AND '.($idColumn ?: 'id').' <> ?';
}

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();
}

/**
Expand Down