Skip to content

Commit

Permalink
Fixed the search engine for PostgreSQL databases
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Jul 14, 2015
1 parent 418a2ad commit 4890eca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace JavierEguiluz\Bundle\EasyAdminBundle\Controller;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand Down Expand Up @@ -506,11 +507,17 @@ protected function findAll($entityClass, $page = 1, $maxPerPage = 15, $sortField
*/
protected function findBy($entityClass, $searchQuery, array $searchableFields, $page = 1, $maxPerPage = 15)
{
$dbIsPostgreSql = $this->isPostgreSqlUsedByEntity($entityClass);
$queryBuilder = $this->em->createQueryBuilder()->select('entity')->from($entityClass, 'entity');

$queryConditions = $queryBuilder->expr()->orX();
$queryParameters = array();
foreach ($searchableFields as $name => $metadata) {
// PostgreSQL doesn't allow to compare strings values with non-string columns (e.g. 'id')
if ($dbIsPostgreSql && !in_array($metadata['type'], array('text', 'string'))) {
continue;
}

if (in_array($metadata['dataType'], array('text', 'string'))) {
$queryConditions->add(sprintf('entity.%s LIKE :query', $name));
$queryParameters['query'] = '%'.$searchQuery.'%';
Expand Down Expand Up @@ -697,4 +704,18 @@ public function renderCssAction()

return $response;
}

/**
* Returns true if the data of the given entity are stored in a database
* of Type PostgreSQL.
*
* @param string $entityClass
* @return boolean
*/
private function isPostgreSqlUsedByEntity($entityClass)
{
$em = $this->get('doctrine')->getManagerForClass($entityClass);

return $em->getConnection()->getDatabasePlatform() instanceof PostgreSqlPlatform;
}
}

0 comments on commit 4890eca

Please sign in to comment.