Skip to content

Commit f208a0e

Browse files
committed
First step at making hydration the default when querying a table
1 parent ddcccc8 commit f208a0e

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

Cake/ORM/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Query extends DatabaseQuery {
122122
*
123123
* @var boolean
124124
*/
125-
protected $_hydrate = false;
125+
protected $_hydrate = true;
126126

127127
/**
128128
* @param Cake\Database\Connection $connection

Cake/Test/TestCase/ORM/QueryTest.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ public function testContainToJoinsOneLevel() {
179179

180180
$s = $query
181181
->select('foo.id')
182-
->repository($this->table)
183182
->contain($contains)->sql();
184183
}
185184

@@ -255,10 +254,11 @@ public function testContainToFieldsDefault() {
255254

256255
/**
257256
* Tests that results are grouped correctly when using contain()
257+
* and results are not hydrated
258258
*
259259
* @return void
260260
**/
261-
public function testContainResultFetchingOneLevel() {
261+
public function testContainResultFetchingOneLevelNoHydration() {
262262
$this->_createTables();
263263

264264
$table = Table::build('article', ['table' => 'articles']);
@@ -268,6 +268,7 @@ public function testContainResultFetchingOneLevel() {
268268
$query = new Query($this->connection, $table);
269269
$results = $query->select()
270270
->contain('author')
271+
->hydrate(false)
271272
->order(['article.id' => 'asc'])
272273
->toArray();
273274
$expected = [
@@ -318,14 +319,15 @@ public function strategiesProvider() {
318319
}
319320

320321
/**
321-
* Tests that HasMany associations are correctly eager loaded.
322+
* Tests that HasMany associations are correctly eager loaded and results
323+
* correctly nested when no hydration is used
322324
* Also that the query object passes the correct parent model keys to the
323325
* association objects in order to perform eager loading with select strategy
324326
*
325327
* @dataProvider strategiesProvider
326328
* @return void
327329
**/
328-
public function testHasManyEagerLoading($strategy) {
330+
public function testHasManyEagerLoadingNoHydration($strategy) {
329331
$this->_createTables();
330332

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

340-
$results = $query->select()->contain('article')->toArray();
342+
$results = $query->select()
343+
->contain('article')
344+
->hydrate(false)
345+
->toArray();
341346
$expected = [
342347
[
343348
'id' => 1,
@@ -386,6 +391,7 @@ public function testHasManyEagerLoading($strategy) {
386391
$results = $query->repository($table)
387392
->select()
388393
->contain(['article' => ['conditions' => ['id' => 2]]])
394+
->hydrate(false)
389395
->toArray();
390396
unset($expected[0]['articles']);
391397
$this->assertEquals($expected, $results);
@@ -398,7 +404,7 @@ public function testHasManyEagerLoading($strategy) {
398404
* @dataProvider strategiesProvider
399405
* @return void
400406
**/
401-
public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
407+
public function testHasManyEagerLoadingFieldsAndOrderNoHydration($strategy) {
402408
$this->_createTables();
403409

404410
$table = Table::build('author', ['connection' => $this->connection]);
@@ -413,6 +419,7 @@ public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
413419
'sort' => ['id' => 'DESC']
414420
]
415421
])
422+
->hydrate(false)
416423
->toArray();
417424
$expected = [
418425
[
@@ -448,7 +455,7 @@ public function testHasManyEagerLoadingFieldsAndOrder($strategy) {
448455
* @dataProvider strategiesProvider
449456
* @return void
450457
**/
451-
public function testHasManyEagerLoadingDeep($strategy) {
458+
public function testHasManyEagerLoadingDeepNoHydration($strategy) {
452459
$this->_createTables();
453460

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

464471
$results = $query->select()
465472
->contain(['article' => ['author']])
473+
->hydrate(false)
466474
->toArray();
467475
$expected = [
468476
[
@@ -535,6 +543,7 @@ public function testHasManyEagerLoadingFromSecondaryTable($strategy) {
535543
$results = $query->select()
536544
->contain(['author' => ['post']])
537545
->order(['article.id' => 'ASC'])
546+
->hydrate(false)
538547
->toArray();
539548
$expected = [
540549
[
@@ -623,7 +632,7 @@ public function testHasManyEagerLoadingFromSecondaryTable($strategy) {
623632
* @dataProvider strategiesProvider
624633
* @return void
625634
**/
626-
public function testBelongsToManyEagerLoading($strategy) {
635+
public function testBelongsToManyEagerLoadingNoHydration($strategy) {
627636
$this->_createTables();
628637

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

638-
$results = $query->select()->contain('Tag')->toArray();
647+
$results = $query->select()->contain('Tag')->hydrate(false)->toArray();
639648
$expected = [
640649
[
641650
'id' => 1,
@@ -687,6 +696,7 @@ public function testBelongsToManyEagerLoading($strategy) {
687696

688697
$results = $query->select()
689698
->contain(['Tag' => ['conditions' => ['id' => 3]]])
699+
->hydrate(false)
690700
->toArray();
691701
$expected = [
692702
[
@@ -727,7 +737,7 @@ public function testBelongsToManyEagerLoading($strategy) {
727737
*
728738
* @return void
729739
*/
730-
public function testFilteringByHasMany() {
740+
public function testFilteringByHasManyNoHydration() {
731741
$this->_createTables();
732742

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

738748
$results = $query->repository($table)
739749
->select()
750+
->hydrate(false)
740751
->contain(['article' => [
741752
'matching' => true,
742753
'conditions' => ['article.id' => 2]
@@ -765,7 +776,7 @@ public function testFilteringByHasMany() {
765776
*
766777
* @return void
767778
**/
768-
public function testFilteringByBelongsToMany() {
779+
public function testFilteringByBelongsToManyNoHydration() {
769780
$this->_createTables();
770781

771782
$query = new Query($this->connection, $this->table);
@@ -782,6 +793,7 @@ public function testFilteringByBelongsToMany() {
782793
'matching' => true,
783794
'conditions' => ['Tag.id' => 3]
784795
]])
796+
->hydrate(false)
785797
->toArray();
786798
$expected = [
787799
[
@@ -804,6 +816,7 @@ public function testFilteringByBelongsToMany() {
804816
'matching' => true,
805817
'conditions' => ['Tag.name' => 'tag2']]
806818
])
819+
->hydrate(false)
807820
->toArray();
808821
$expected = [
809822
[
@@ -1043,7 +1056,7 @@ public function testFirstDirtyQuery() {
10431056
$this->_createTables();
10441057
$table = Table::build('article', ['table' => 'articles']);
10451058
$query = new Query($this->connection, $table);
1046-
$result = $query->select(['id'])->first();
1059+
$result = $query->select(['id'])->hydrate(false)->first();
10471060
$this->assertEquals(['id' => 1], $result);
10481061
$this->assertEquals(1, $query->clause('limit'));
10491062
$result = $query->select(['id'])->first();
@@ -1061,7 +1074,7 @@ public function testFirstCleanQuery() {
10611074
$query = new Query($this->connection, $table);
10621075
$query->select(['id'])->toArray();
10631076

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

1080-
$first = $query->first();
1093+
$first = $query->hydrate(false)->first();
10811094
$resultSet = $query->execute();
10821095
$this->assertEquals(['id' => 1], $first);
10831096
$this->assertSame($resultSet, $query->execute());
@@ -1092,7 +1105,7 @@ public function testHydrateSimple() {
10921105
$this->_createTables();
10931106
$table = Table::build('article', ['table' => 'articles']);
10941107
$query = new Query($this->connection, $table);
1095-
$results = $query->select()->hydrate(true)->execute()->toArray();
1108+
$results = $query->select()->execute()->toArray();
10961109

10971110
$this->assertCount(3, $results);
10981111
foreach ($results as $r) {
@@ -1124,7 +1137,6 @@ public function testHydrateWithHasMany() {
11241137
$query = new Query($this->connection, $table);
11251138
$results = $query->select()
11261139
->contain('article')
1127-
->hydrate(true)
11281140
->toArray();
11291141

11301142
$first = $results[0];
@@ -1171,7 +1183,6 @@ public function testHydrateBelongsToMany() {
11711183
$results = $query
11721184
->select()
11731185
->contain('Tag')
1174-
->hydrate(true)
11751186
->toArray();
11761187

11771188
$first = $results[0];
@@ -1211,7 +1222,6 @@ public function testHydrateBelongsTo() {
12111222
$results = $query->select()
12121223
->contain('author')
12131224
->order(['article.id' => 'asc'])
1214-
->hydrate(true)
12151225
->toArray();
12161226

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

12411251
$results = $query->select()
12421252
->contain(['article' => ['author']])
1243-
->hydrate(true)
12441253
->toArray();
12451254

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

12691278
$this->assertCount(3, $results);
12701279
foreach ($results as $r) {
@@ -1305,7 +1314,6 @@ public function testHydrateWithHasManyCustomEntity() {
13051314
$query = new Query($this->connection, $table);
13061315
$results = $query->select()
13071316
->contain('article')
1308-
->hydrate(true)
13091317
->toArray();
13101318

13111319
$first = $results[0];
@@ -1345,7 +1353,6 @@ public function testHydrateBelongsToCustomEntity() {
13451353
$results = $query->select()
13461354
->contain('author')
13471355
->order(['article.id' => 'asc'])
1348-
->hydrate(true)
13491356
->toArray();
13501357

13511358
$first = $results[0];

0 commit comments

Comments
 (0)