From d7e7baf2a26976e348bc034734f1666a523e57ae Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 8 Jan 2016 17:56:41 +0100 Subject: [PATCH 1/2] Regression test: HAVING clause does not translate variable name when used with * and / math operators --- .../ORM/Query/SelectSqlGenerationTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 6166a81ad..1aabaa78b 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -2271,6 +2271,27 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase 'SELECT COUNT(c0_.name) AS sclr_0 FROM cms_users c0_ HAVING sclr_0 IS NULL' ); } + + /** + * GitHub issue #4764: https://github.com/doctrine/doctrine2/issues/4764 + * @group DDC-3907 + * @dataProvider mathematicOperatorsProvider + */ + public function testHavingRegressionUsingVariableWithMathOperatorsExpression($operator) + { + $this->assertSqlGeneration( + 'SELECT COUNT(u.name) AS countName FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING 1 ' . $operator . ' countName > 0', + 'SELECT COUNT(c0_.name) AS sclr_0 FROM cms_users c0_ HAVING 1 ' . $operator . ' sclr_0 > 0' + ); + } + + /** + * @return array + */ + public function mathematicOperatorsProvider() + { + return [['+'], ['-'], ['*'], ['/']]; + } } class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode From 55d4f515af82d87120f1edfe35c60f078ee97892 Mon Sep 17 00:00:00 2001 From: Bill Schaller Date: Fri, 8 Jan 2016 12:53:05 -0500 Subject: [PATCH 2/2] Fix issue were identifier operands in /,* arithmetic terms were not checked to see if they're query components --- lib/Doctrine/ORM/Query/SqlWalker.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 92cb3d757..0fb389de0 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -2272,7 +2272,9 @@ class SqlWalker implements TreeWalker public function walkArithmeticFactor($factor) { if (is_string($factor)) { - return $factor; + return (isset($this->queryComponents[$factor])) + ? $this->walkResultVariable($this->queryComponents[$factor]['token']['value']) + : $factor; } // Phase 2 AST optimization: Skip processing of ArithmeticFactor