Skip to content

Commit

Permalink
bug #381 Fixed the search engine for PostgreSQL databases (javieregui…
Browse files Browse the repository at this point in the history
…luz)

This PR was squashed before being merged into the master branch (closes #381).

Discussion
----------

Fixed the search engine for PostgreSQL databases

This fixes #372

Commits
-------

4890eca Fixed the search engine for PostgreSQL databases
  • Loading branch information
javiereguiluz committed Jul 14, 2015
2 parents 0b5e26a + 4890eca commit 7c7b52f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Controller/AdminController.php
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 7c7b52f

Please sign in to comment.