diff --git a/Doctrine/Hydrate.php b/Doctrine/Hydrate.php index e3a805dbe..9ec2f2e47 100644 --- a/Doctrine/Hydrate.php +++ b/Doctrine/Hydrate.php @@ -255,13 +255,17 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { $keys = array_keys($this->tables); $name = $this->tables[$keys[0]]->getComponentName(); + + $stmt = $this->connection->execute($query,$params); while($data = $stmt->fetch(PDO::FETCH_ASSOC)): + foreach($data as $key => $value): $e = explode("__",$key); if(count($e) > 1) { - $data[$e[1]] = $value; + + $data[end($e)] = $value; } else { $data[$e[0]] = $value; } @@ -269,6 +273,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { endforeach; $this->data[$name][] = $data; endwhile; + return $this->getCollection($keys[0]); break; @@ -278,7 +283,9 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { if($this->isLimitSubqueryUsed()) $params = array_merge($params, $params); + + $stmt = $this->connection->execute($query,$params); $previd = array(); @@ -292,6 +299,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { return $this->hydrateHolders($array); } elseif($return == Doctrine::FETCH_ARRAY) return $array; + + foreach($array as $data) { /** @@ -504,15 +513,15 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { foreach($data as $key => $value): $e = explode("__",$key); - if(count($e) > 1) { - $data[$e[0]][$e[1]] = $value; - } else { - $data[0][$e[0]] = $value; - } + $field = array_pop($e); + $component = implode("__",$e); + $data[$component][$field] = $value; + unset($data[$key]); endforeach; $array[] = $data; endwhile; + $stmt->closeCursor(); return $array; } diff --git a/Doctrine/RawSql.php b/Doctrine/RawSql.php index 5a8053c2c..ae70eb353 100644 --- a/Doctrine/RawSql.php +++ b/Doctrine/RawSql.php @@ -94,8 +94,10 @@ class Doctrine_RawSql extends Doctrine_Hydrate { case "group": $i = ($k + 1); if(isset($e[$i]) && strtolower($e[$i]) === "by") { - $p = $part; - $parts[$low] = array(); + $p = $low; + $p .= "by"; + $parts[$low."by"] = array(); + } else $parts[$p][] = $part; break; @@ -121,7 +123,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate { * @return string */ public function getQuery() { - $q = array(); + foreach($this->fields as $field) { $e = explode(".", $field); @@ -157,7 +159,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate { } } - $q[] = "SELECT ".implode(', ', $this->parts['select']); + $q = "SELECT ".implode(', ', $this->parts['select']); $string = $this->applyInheritance(); if( ! empty($string)) @@ -165,13 +167,17 @@ class Doctrine_RawSql extends Doctrine_Hydrate { $copy = $this->parts; unset($copy['select']); - foreach($copy as $name => $part) { - if(empty($part)) - continue; - $q[] = strtoupper($name).' '.implode(' ',$part); - } - return implode(' ', $q); + $q .= ( ! empty($this->parts['from']))?" FROM ".implode(" ",$this->parts["from"]):''; + $q .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; + $q .= ( ! empty($this->parts['groupby']))?" GROUP BY ".implode(", ",$this->parts["groupby"]):''; + $q .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):''; + $q .= ( ! empty($this->parts['orderby']))?" ORDER BY ".implode(" ",$this->parts["orderby"]):''; + + if( ! empty($string)) + array_pop($this->parts['where']); + + return $q; } /** * getFields diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 82e1eedcf..0c4263788 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -759,10 +759,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { if( ! is_array($key)) $key = array($key); - foreach($key as $k) { if( ! isset($this->data[$k])) - throw new Doctrine_Exception("No primary key found"); + throw new Doctrine_Exception("Primary key value for $k wasn't found"); $id[] = $this->data[$k]; } diff --git a/Doctrine/Validator/Notblank.php b/Doctrine/Validator/Notblank.php index 412556c34..760221e12 100644 --- a/Doctrine/Validator/Notblank.php +++ b/Doctrine/Validator/Notblank.php @@ -8,7 +8,7 @@ class Doctrine_Validator_Notblank { * @return boolean */ public function validate(Doctrine_Record $record, $key, $value, $args) { - return (trim($var) != ""); + return (trim($value) != ""); } } ?> diff --git a/tests/ManagerTestCase.php b/tests/ManagerTestCase.php index 757ac1be5..f9d052177 100644 --- a/tests/ManagerTestCase.php +++ b/tests/ManagerTestCase.php @@ -18,6 +18,13 @@ class Doctrine_ManagerTestCase extends Doctrine_UnitTestCase { } public function testGetConnections() { $this->assertEqual(count($this->manager->getConnections()),1); + } + public function testClassifyTableize() { + $name = "Forum_Category"; + $this->assertEqual(Doctrine::tableize($name), "forum__category"); + $this->assertEqual(Doctrine::classify(Doctrine::tableize($name)), $name); + + } public function prepareData() { } public function prepareTables() { } diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index 0c11071b5..bf2b44e5b 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -110,7 +110,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $query->where("(((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'))"); $this->assertEqual($query->getQuery(), $sql); } - public function testSelfReferencing() { $query = new Doctrine_Query($this->connection); @@ -187,7 +186,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($count, count($this->dbh)); - $query->from("Forum_Category.Parent, Forum_Category.Subcategory")->where("Forum_Category.name = 'Sub 1' OR Forum_Category.name = 'Sub 2'"); + $query->from("Forum_Category.Parent, Forum_Category.Subcategory")->where("Forum_Category.name = 'Sub 1' || Forum_Category.name = 'Sub 2'"); $coll = $query->execute(); @@ -891,19 +890,19 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $table = $this->connection->getTable("Forum_Thread")->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY); $q->from("Forum_Board.Threads"); - $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id"); + $this->assertEqual($q->getQuery(), "SELECT forum__board.id AS forum__board__id, forum__thread.id AS forum__thread__id FROM forum__board LEFT JOIN forum__thread ON forum__board.id = forum__thread.board_id"); $coll = $q->execute(); $this->assertEqual($coll->count(), 1); $q->from("Forum_Board-l.Threads-l"); - $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id"); + $this->assertEqual($q->getQuery(), "SELECT forum__board.id AS forum__board__id, forum__thread.id AS forum__thread__id FROM forum__board LEFT JOIN forum__thread ON forum__board.id = forum__thread.board_id"); //$this->connection->clear(); $q->from("Forum_Board-l.Threads-l.Entries-l"); - $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id, forum_entry.id AS forum_entry__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id LEFT JOIN forum_entry ON forum_thread.id = forum_entry.thread_id"); + $this->assertEqual($q->getQuery(), "SELECT forum__board.id AS forum__board__id, forum__thread.id AS forum__thread__id, forum__entry.id AS forum__entry__id FROM forum__board LEFT JOIN forum__thread ON forum__board.id = forum__thread.board_id LEFT JOIN forum__entry ON forum__thread.id = forum__entry.thread_id"); $boards = $q->execute(); $this->assertEqual($boards->count(), 1); $count = count($this->dbh); diff --git a/tests/RawSqlTestCase.php b/tests/RawSqlTestCase.php index 0020efb2c..f991e8ac0 100644 --- a/tests/RawSqlTestCase.php +++ b/tests/RawSqlTestCase.php @@ -1,5 +1,6 @@ connection); @@ -114,6 +115,7 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase { $this->assertTrue(is_numeric($coll[3]->id)); $this->assertTrue(is_numeric($coll[7]->id)); } + public function testColumnAggregationInheritance() { // forcing the select of primary key fields @@ -128,5 +130,26 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase { $this->assertTrue(is_numeric($coll[3]->id)); $this->assertTrue(is_numeric($coll[7]->id)); } + + public function testColumnAggregationInheritanceWithOrderBy() { + // forcing the select of primary key fields + + $query = new Doctrine_RawSql($this->connection); + + $query->parseQuery("SELECT {entity.name} FROM entity ORDER BY entity.name"); + $query->addComponent("entity", "User"); + + $this->assertEqual($query->getQuery(), "SELECT entity.name AS entity__name, entity.id AS entity__id FROM entity WHERE entity.type = 0 ORDER BY entity.name"); + + + $coll = $query->execute(); + + $this->assertEqual($coll->count(), 8); + $this->assertTrue(is_numeric($coll[0]->id)); + $this->assertTrue(is_numeric($coll[3]->id)); + $this->assertTrue(is_numeric($coll[7]->id)); + + } + } ?> diff --git a/tests/run.php b/tests/run.php index 6319f410b..93eb25b2d 100644 --- a/tests/run.php +++ b/tests/run.php @@ -28,15 +28,15 @@ require_once("QueryLimitTestCase.php"); error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); - +/** $test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_ConnectionTestCase()); $test->addTestCase(new Doctrine_TableTestCase()); - +*/ $test->addTestCase(new Doctrine_ManagerTestCase()); - +/** $test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_EventListenerTestCase()); @@ -63,12 +63,12 @@ $test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); -$test->addTestCase(new Doctrine_QueryTestCase()); +$test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_RawSql_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase()); - +*/ //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());