From b0202f2921a570e8bf23d62033c4e689c106c74a Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Fri, 14 Aug 2009 21:03:27 +0000 Subject: [PATCH] [2.0] Fixed issue with missing parenthesis in Math expressions --- lib/Doctrine/ORM/Query/Expr/Math.php | 16 +++++++++++++++- tests/Doctrine/Tests/ORM/Query/ExprTest.php | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/Expr/Math.php b/lib/Doctrine/ORM/Query/Expr/Math.php index 89de7a599..ffceaf880 100644 --- a/lib/Doctrine/ORM/Query/Expr/Math.php +++ b/lib/Doctrine/ORM/Query/Expr/Math.php @@ -45,6 +45,20 @@ class Math public function __toString() { - return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr; + // Adjusting Left Expression + $leftExpr = (string) $this->_leftExpr; + + if ($this->_leftExpr instanceof Math) { + $leftExpr = '(' . $leftExpr . ')'; + } + + // Adjusting Right Expression + $rightExpr = (string) $this->_rightExpr; + + if ($this->_rightExpr instanceof Math) { + $rightExpr = '(' . $rightExpr . ')'; + } + + return $leftExpr . ' ' . $this->_operator . ' ' . $rightExpr; } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/ExprTest.php b/tests/Doctrine/Tests/ORM/Query/ExprTest.php index 942196c8c..6fc3b7ea6 100644 --- a/tests/Doctrine/Tests/ORM/Query/ExprTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ExprTest.php @@ -129,6 +129,12 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase { $this->assertEquals('10 / 2', (string) Expr::quot(10, 2)); } + + public function testScopeInArithmeticExpr() + { + $this->assertEquals('(100 - 20) / 2', (string) Expr::quot(Expr::diff(100, 20), 2)); + $this->assertEquals('100 - (20 / 2)', (string) Expr::diff(100, Expr::quot(20, 2))); + } public function testSquareRootExpr() {