mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-670: Mysql function CONCAT doesn't allow more than 2 parameters #823
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Jul 6, 2010).
Jira issue originally created by user jklehr:
Trying to use CONCAT() function in a DQL query that has three (or more) parameters with mysql and getting a parser exception:
"Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','"
Mysql supports 2 or more parameters in the CONCAT function.
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
@doctrinebot commented on GitHub (Jul 6, 2010):
Comment created by @beberlei:
Yes, but only MySQL.
We have to support the common denominator across all our supported vendors.
You can either extend the concat function and overwrite the existing one as detailed in the documentation:
http://www.doctrine-project.org/projects/orm/2.0/docs/cookbook/dql-user-defined-functions/en#dql-user-defined-functions
Or use the following tirck:
@doctrinebot commented on GitHub (Jul 6, 2010):
Issue was closed with resolution "Won't Fix"
@doctrinebot commented on GitHub (Jul 6, 2010):
Comment created by jklehr:
All other DB engines support concatenation of multiple strings/fields they just do it differently. Many DB engines don't have a CONCAT function at all, so what is the parser doing to support them?
double pipe operator (standard SQL):
Supported by: Oracle, Postgres, Firebird, IBM DB2, Informix, SQLite
operator:
Supported by: MSSQL
CONCAT function (only two parameters):
Supported by: Oracle, 4D, Mysql
CONCAT function (more than two parameters):
Supported by: Mysql
I assume the parser is converting "CONCAT()" in DQL to the proper underlying DB engine's alternative since CONCAT isn't supported by many DB engines natively. Couldn't the parser convert "CONCAT(first_name, ' ', last_name)" into the supported SQL for the DB engine in use? The only "trick" would be for 4D (using multiple CONCATs).
I know DQL isn't SQL, but what version of SQL did you decide to mimic for these functions? Will the parser choke on "||" thinking it's an OR clause (like mysql)?
@doctrinebot commented on GitHub (Jun 5, 2012):
Comment created by tridem:
Try combining multiple CONCAT with quotes escaped:
CONCAT(CONCAT(cp.lastName, ', '), cp.firstName) AS label
Havn't found a "cleaner" solution yet.
@crayner commented on GitHub (Jul 14, 2017):
solution I use in Doctrine 2.4+:
$concat = new Query\Expr\Func('CONCAT', $name[$k]);
$concat .= ' as ' . $k;
$concat = str_replace(',', ',' ',', $concat);
$this->query->addSelect($concat);
so $name[$k] is an array of fields, as many as you wish. I then add some spacing between the fields with the str_replace. Hope this helps someone.
Craig