From e4114c47fac9b10e895953aa9633a04d41bb99a9 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 5 Jul 2007 17:25:53 +0000 Subject: [PATCH] fixed literal enum conversion with joins --- lib/Doctrine/Query.php | 13 ++++----- lib/Doctrine/Query/Where.php | 2 +- tests/EnumTestCase.php | 52 +++++++++++++++++------------------- tests/classes.php | 6 ++--- tests/run.php | 4 +-- 5 files changed, 36 insertions(+), 41 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 7f1080fdc..532f815f1 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -1098,8 +1098,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $path = $e[0]; } - $tmp = explode(' ', $path); - $originalAlias = (count($tmp) > 1) ? end($tmp) : null; + $tmp = explode(' ', $path); + $componentAlias = $originalAlias = (count($tmp) > 1) ? end($tmp) : null; $e = preg_split("/[.:]/", $tmp[0], -1); @@ -1109,6 +1109,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable if (isset($this->_aliasMap[$e[0]])) { $table = $this->_aliasMap[$e[0]]['table']; + $componentAlias = $e[0]; $prevPath = $parent = array_shift($e); } @@ -1229,11 +1230,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable if(isset($this->pendingFields[$componentAlias])) { $this->processPendingFields($componentAlias); } - /** - if(isset($this->pendingAggregates[$componentAlias]) || isset($this->pendingAggregates[0])) { - $this->processPendingAggregates($componentAlias); - } - */ if ($restoreState) { $this->pendingFields = array(); @@ -1242,7 +1238,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } $parent = $prevPath; } - return end($this->_aliasMap); + + return $this->_aliasMap[$componentAlias]; } /** * loadRoot diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 6874269a8..56ba4e8a7 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -126,7 +126,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition $field = $table->getColumnName($field); // check if value is enumerated value $enumIndex = $table->enumIndex($field, trim($value, "'")); - + if (substr($value, 0, 1) == '(') { // trim brackets $trimmed = Doctrine_Tokenizer::bracketTrim($value); diff --git a/tests/EnumTestCase.php b/tests/EnumTestCase.php index 046cc1fd4..ce6cf5457 100644 --- a/tests/EnumTestCase.php +++ b/tests/EnumTestCase.php @@ -39,7 +39,7 @@ class Doctrine_Enum_TestCase extends Doctrine_UnitTestCase $this->tables = array("EnumTest", "EnumTest2", "EnumTest3"); parent::prepareTables(); } - + /** public function testParameterConversion() { $test = new EnumTest(); @@ -66,17 +66,7 @@ class Doctrine_Enum_TestCase extends Doctrine_UnitTestCase } } - public function testEnumFetchArray() { - $q = new Doctrine_Query(); - $q->select('e.*') - ->from('EnumTest e') - ->limit(1); - $ret = $q->execute(array(), Doctrine::FETCH_ARRAY); - if (is_numeric($ret[0]['status'])) - { - $this->fail(); - } - } + public function testInAndNotIn() { @@ -117,7 +107,7 @@ class Doctrine_Enum_TestCase extends Doctrine_UnitTestCase $this->fail(); } } - + */ public function testEnumType() { @@ -183,22 +173,30 @@ class Doctrine_Enum_TestCase extends Doctrine_UnitTestCase $this->assertTrue($f); } - public function testEnumWithLeftJoin() - { - // sorry, odd I know, best I can do... pookey! - try { - $q = new Doctrine_Query($this->connection); - $q->select('e.*, f.*, g.status') - ->from('EnumTest e') - ->innerjoin('e.Enum2 g') - ->leftjoin('e.Enum3 f') - ->where("e.status = 'verified'") - ->limit(1) - ->execute(); - $this->assertNotEqual($q->getQuery(), "SELECT e.id AS e__id, e.status AS e__status, e.text AS e__text, e2.id AS e2__id, e2.status AS e2__status, e3.text AS e3__text FROM enum_test e INNER JOIN enum_test2 e2 ON e.id = e2.enum_test_id LEFT JOIN enum_test3 e3 ON e.text = e3.text WHERE e.id IN (SELECT DISTINCT e4.id FROM enum_test e4 INNER JOIN enum_test2 e5 ON e4.id = e5.enum_test_id LEFT JOIN enum_test3 e6 ON e4.text = e6.text WHERE e4.status = 'verified' LIMIT 1) AND e.status = 'verified'"); - } catch (Exception $e) { + public function testEnumFetchArray() { + $q = new Doctrine_Query(); + $q->select('e.*') + ->from('EnumTest e') + ->limit(1); + $ret = $q->execute(array(), Doctrine::FETCH_ARRAY); + + if (is_numeric($ret[0]['status'])) { $this->fail(); } } + + public function testLiteralEnumValueConversionSupportsJoins() + { + + $q = new Doctrine_Query($this->connection); + $q->addSelect('e.*') + ->addSelect('e3.*') + ->from('EnumTest e') + ->leftjoin('e.Enum3 e3') + ->where("e.status = 'verified'") + ->execute(); + + $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.status AS e__status, e.text AS e__text, e2.text AS e2__text FROM enum_test e LEFT JOIN enum_test3 e2 ON e.text = e2.text WHERE e.status = 1"); + } } ?> diff --git a/tests/classes.php b/tests/classes.php index 8a73af981..3a7d8e799 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -417,8 +417,8 @@ class EnumTest extends Doctrine_Record $this->hasColumn('text', 'string'); } public function setUp() { - $this->ownsMany('EnumTest2 as Enum2', array('local' => 'id', 'foreign' => 'enum_test_id')); - $this->ownsMany('EnumTest3 as Enum3', array('local' => 'text', 'foreign' => 'text')); + $this->hasMany('EnumTest2 as Enum2', array('local' => 'id', 'foreign' => 'enum_test_id')); + $this->hasMany('EnumTest3 as Enum3', array('local' => 'text', 'foreign' => 'text')); } } class EnumTest2 extends Doctrine_Record @@ -666,7 +666,7 @@ class NestTest extends Doctrine_Record } public function setUp() { - $this->hasMany('NestTest as Parents', array('local' => 'child_id', + $this->hasMany('NestTest as Parents', array('local' => 'child_id', 'refClass' => 'NestReference', 'foreign' => 'parent_id')); $this->hasMany('NestTest as Children', array('local' => 'parent_id', diff --git a/tests/run.php b/tests/run.php index 92adf7e4c..7ca21c154 100644 --- a/tests/run.php +++ b/tests/run.php @@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); $test->addTestCase(new Doctrine_Ticket330_TestCase()); */ -/***/ +/** */ // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); @@ -181,7 +181,7 @@ $test->addTestCase(new Doctrine_Relation_Parser_TestCase()); // Datatypes $test->addTestCase(new Doctrine_Enum_TestCase()); - + $test->addTestCase(new Doctrine_Boolean_TestCase());