EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

EntityManager::addSearchClause throw exception on Postgresql databases #6290

Closed gregGit closed 2 weeks ago

gregGit commented 2 weeks ago

Describe the bug When you a search is done using the search bar, on postgresql a SQL error is thrown when a searchable field is an integer

An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: function lower(integer) does not exist
LINE 1: .. WHERE LOWER(o0_....
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

To Reproduce Every search with easyadmin 4.10.0 when a integer field is inclued in the search, and using a postgresql database. Why ? Because postgresql doesn't support lower(field) if field is a int, because lower function in postsgresql require a varchar value (and so postgresql throw a error ERROR: function lower(integer) does not exist)

(OPTIONAL) Additional context The bug is on the file EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository On recent version L123 was modified from elseif ($propertyConfig['is_text']) to elseif ($propertyConfig['is_text'] || $propertyConfig['is_integer'])

So you add the ability to use a like search on a integer field, but this doesn't work on postgresql DB. (but probably work fine on Mysql/mariaDb)

A solution can be to modify L125 from $queryTermConditions->add(sprintf('LOWER(%s.%s) LIKE :%s', $entityName, $propertyConfig['property_name'], $parameterName)); to $queryTermConditions->add(sprintf("LOWER(CONCAT(%s.%s, '')) LIKE :%s", $entityName, $propertyConfig['property_name'], $parameterName)); Explanation : By using CONCAT(%s.%s, '') we don't change the fied value, but the result of concat is a varchar, so LOWER works fine.

javiereguiluz commented 2 weeks ago

@gregGit thanks for reporting this error and for the detailed explanation.

I'm using your proposed fix in #6292. I'd like to rework this to use a different solution that doesn't require that concatenation ... but I need this bug fix urgently, so let's use it for now. Thanks!