Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First step at making hydration the default when querying a table
  • Loading branch information
lorenzo committed Oct 18, 2013
1 parent ddcccc8 commit f208a0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cake/ORM/Query.php
Expand Up @@ -122,7 +122,7 @@ class Query extends DatabaseQuery {
*
* @var boolean
*/
protected $_hydrate = false;
protected $_hydrate = true;

/**
* @param Cake\Database\Connection $connection
Expand Down
51 changes: 29 additions & 22 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -179,7 +179,6 @@ public function testContainToJoinsOneLevel() {

$s = $query
->select('foo.id')
->repository($this->table)
->contain($contains)->sql();
}

Expand Down Expand Up @@ -255,10 +254,11 @@ public function testContainToFieldsDefault() {

/**
* Tests that results are grouped correctly when using contain()
* and results are not hydrated
*
* @return void
**/
public function testContainResultFetchingOneLevel() {
public function testContainResultFetchingOneLevelNoHydration() {
$this->_createTables();

$table = Table::build('article', ['table' => 'articles']);
Expand All @@ -268,6 +268,7 @@ public function testContainResultFetchingOneLevel() {
$query = new Query($this->connection, $table);
$results = $query->select()
->contain('author')
->hydrate(false)
->order(['article.id' => 'asc'])
->toArray();
$expected = [
Expand Down Expand Up @@ -318,14 +319,15 @@ public function strategiesProvider() {
}

/**
* Tests that HasMany associations are correctly eager loaded.
* Tests that HasMany associations are correctly eager loaded and results
* correctly nested when no hydration is used
* Also that the query object passes the correct parent model keys to the
* association objects in order to perform eager loading with select strategy
*
* @dataProvider strategiesProvider
* @return void
**/
public function testHasManyEagerLoading($strategy) {
public function testHasManyEagerLoadingNoHydration($strategy) {
$this->_createTables();

$table = Table::build('author', ['connection' => $this->connection]);
Expand All @@ -337,7 +339,10 @@ public function testHasManyEagerLoading($strategy) {
]);
$query = new Query($this->connection, $table);

$results = $query->select()->contain('article')->toArray();
$results = $query->select()
->contain('article')
->hydrate(false)
->toArray();
$expected = [
[
'id' => 1,
Expand Down Expand Up @@ -386,6 +391,7 @@ public function testHasManyEagerLoading($strategy) {
$results = $query->repository($table)
->select()
->contain(['article' => ['conditions' => ['id' => 2]]])
->hydrate(false)
->toArray();
unset($expected[0]['articles']);
$this->assertEquals($expected, $results);
Expand All @@ -398,7 +404,7 @@ public function testHasManyEagerLoading($strategy) {
* @dataProvider strategiesProvider
* @return void
**/
public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
public function testHasManyEagerLoadingFieldsAndOrderNoHydration($strategy) {
$this->_createTables();

$table = Table::build('author', ['connection' => $this->connection]);
Expand All @@ -413,6 +419,7 @@ public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
'sort' => ['id' => 'DESC']
]
])
->hydrate(false)
->toArray();
$expected = [
[
Expand Down Expand Up @@ -448,7 +455,7 @@ public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
* @dataProvider strategiesProvider
* @return void
**/
public function testHasManyEagerLoadingDeep($strategy) {
public function testHasManyEagerLoadingDeepNoHydration($strategy) {
$this->_createTables();

$table = Table::build('author', ['connection' => $this->connection]);
Expand All @@ -463,6 +470,7 @@ public function testHasManyEagerLoadingDeep($strategy) {

$results = $query->select()
->contain(['article' => ['author']])
->hydrate(false)
->toArray();
$expected = [
[
Expand Down Expand Up @@ -535,6 +543,7 @@ public function testHasManyEagerLoadingFromSecondaryTable($strategy) {
$results = $query->select()
->contain(['author' => ['post']])
->order(['article.id' => 'ASC'])
->hydrate(false)
->toArray();
$expected = [
[
Expand Down Expand Up @@ -623,7 +632,7 @@ public function testHasManyEagerLoadingFromSecondaryTable($strategy) {
* @dataProvider strategiesProvider
* @return void
**/
public function testBelongsToManyEagerLoading($strategy) {
public function testBelongsToManyEagerLoadingNoHydration($strategy) {
$this->_createTables();

$table = Table::build('Article', ['connection' => $this->connection]);
Expand All @@ -635,7 +644,7 @@ public function testBelongsToManyEagerLoading($strategy) {
$table->belongsToMany('Tag', ['property' => 'tags', 'strategy' => $strategy]);
$query = new Query($this->connection, $table);

$results = $query->select()->contain('Tag')->toArray();
$results = $query->select()->contain('Tag')->hydrate(false)->toArray();
$expected = [
[
'id' => 1,
Expand Down Expand Up @@ -687,6 +696,7 @@ public function testBelongsToManyEagerLoading($strategy) {

$results = $query->select()
->contain(['Tag' => ['conditions' => ['id' => 3]]])
->hydrate(false)
->toArray();
$expected = [
[
Expand Down Expand Up @@ -727,7 +737,7 @@ public function testBelongsToManyEagerLoading($strategy) {
*
* @return void
*/
public function testFilteringByHasMany() {
public function testFilteringByHasManyNoHydration() {
$this->_createTables();

$query = new Query($this->connection, $this->table);
Expand All @@ -737,6 +747,7 @@ public function testFilteringByHasMany() {

$results = $query->repository($table)
->select()
->hydrate(false)
->contain(['article' => [
'matching' => true,
'conditions' => ['article.id' => 2]
Expand Down Expand Up @@ -765,7 +776,7 @@ public function testFilteringByHasMany() {
*
* @return void
**/
public function testFilteringByBelongsToMany() {
public function testFilteringByBelongsToManyNoHydration() {
$this->_createTables();

$query = new Query($this->connection, $this->table);
Expand All @@ -782,6 +793,7 @@ public function testFilteringByBelongsToMany() {
'matching' => true,
'conditions' => ['Tag.id' => 3]
]])
->hydrate(false)
->toArray();
$expected = [
[
Expand All @@ -804,6 +816,7 @@ public function testFilteringByBelongsToMany() {
'matching' => true,
'conditions' => ['Tag.name' => 'tag2']]
])
->hydrate(false)
->toArray();
$expected = [
[
Expand Down Expand Up @@ -1043,7 +1056,7 @@ public function testFirstDirtyQuery() {
$this->_createTables();
$table = Table::build('article', ['table' => 'articles']);
$query = new Query($this->connection, $table);
$result = $query->select(['id'])->first();
$result = $query->select(['id'])->hydrate(false)->first();
$this->assertEquals(['id' => 1], $result);
$this->assertEquals(1, $query->clause('limit'));
$result = $query->select(['id'])->first();
Expand All @@ -1061,7 +1074,7 @@ public function testFirstCleanQuery() {
$query = new Query($this->connection, $table);
$query->select(['id'])->toArray();

$first = $query->first();
$first = $query->hydrate(false)->first();
$this->assertEquals(['id' => 1], $first);
$this->assertNull($query->clause('limit'));
}
Expand All @@ -1077,7 +1090,7 @@ public function testFirstSameResult() {
$query = new Query($this->connection, $table);
$query->select(['id'])->toArray();

$first = $query->first();
$first = $query->hydrate(false)->first();
$resultSet = $query->execute();
$this->assertEquals(['id' => 1], $first);
$this->assertSame($resultSet, $query->execute());
Expand All @@ -1092,7 +1105,7 @@ public function testHydrateSimple() {
$this->_createTables();
$table = Table::build('article', ['table' => 'articles']);
$query = new Query($this->connection, $table);
$results = $query->select()->hydrate(true)->execute()->toArray();
$results = $query->select()->execute()->toArray();

$this->assertCount(3, $results);
foreach ($results as $r) {
Expand Down Expand Up @@ -1124,7 +1137,6 @@ public function testHydrateWithHasMany() {
$query = new Query($this->connection, $table);
$results = $query->select()
->contain('article')
->hydrate(true)
->toArray();

$first = $results[0];
Expand Down Expand Up @@ -1171,7 +1183,6 @@ public function testHydrateBelongsToMany() {
$results = $query
->select()
->contain('Tag')
->hydrate(true)
->toArray();

$first = $results[0];
Expand Down Expand Up @@ -1211,7 +1222,6 @@ public function testHydrateBelongsTo() {
$results = $query->select()
->contain('author')
->order(['article.id' => 'asc'])
->hydrate(true)
->toArray();

$this->assertCount(3, $results);
Expand Down Expand Up @@ -1240,7 +1250,6 @@ public function testHydrateDeep() {

$results = $query->select()
->contain(['article' => ['author']])
->hydrate(true)
->toArray();

$this->assertCount(4, $results);
Expand All @@ -1264,7 +1273,7 @@ public function testHydrateCustomObject() {
'entityClass' => '\\' . $class
]);
$query = new Query($this->connection, $table);
$results = $query->select()->hydrate(true)->execute()->toArray();
$results = $query->select()->execute()->toArray();

$this->assertCount(3, $results);
foreach ($results as $r) {
Expand Down Expand Up @@ -1305,7 +1314,6 @@ public function testHydrateWithHasManyCustomEntity() {
$query = new Query($this->connection, $table);
$results = $query->select()
->contain('article')
->hydrate(true)
->toArray();

$first = $results[0];
Expand Down Expand Up @@ -1345,7 +1353,6 @@ public function testHydrateBelongsToCustomEntity() {
$results = $query->select()
->contain('author')
->order(['article.id' => 'asc'])
->hydrate(true)
->toArray();

$first = $results[0];
Expand Down

0 comments on commit f208a0e

Please sign in to comment.