@@ -62,6 +62,7 @@ public function setUp() {
62
62
public function tearDown () {
63
63
$ this ->connection ->execute ('DROP TABLE IF EXISTS articles ' );
64
64
$ this ->connection ->execute ('DROP TABLE IF EXISTS authors ' );
65
+ $ this ->connection ->execute ('DROP TABLE IF EXISTS publications ' );
65
66
Table::clearRegistry ();
66
67
}
67
68
@@ -70,24 +71,28 @@ public function tearDown() {
70
71
*
71
72
* @return void
72
73
*/
73
- protected function _createAuthorsAndArticles () {
74
+ protected function _createTables () {
74
75
$ table = 'CREATE TEMPORARY TABLE authors(id int, name varchar(50)) ' ;
75
76
$ this ->connection ->execute ($ table );
76
77
77
78
$ table = 'CREATE TEMPORARY TABLE articles(id int, title varchar(20), body varchar(50), author_id int) ' ;
78
79
$ this ->connection ->execute ($ table );
79
80
81
+ $ table = 'CREATE TEMPORARY TABLE publications(id int, title varchar(20), body varchar(50), author_id int) ' ;
82
+ $ this ->connection ->execute ($ table );
83
+
80
84
Table::config ('authors ' , ['connection ' => $ this ->connection ]);
81
85
Table::config ('articles ' , ['connection ' => $ this ->connection ]);
86
+ Table::config ('publications ' , ['connection ' => $ this ->connection ]);
82
87
}
83
88
84
89
/**
85
90
* Auxiliary function to insert a couple rows in a newly created table
86
91
*
87
92
* @return void
88
93
*/
89
- protected function _insertTwoRecords () {
90
- $ this ->_createAuthorsAndArticles ();
94
+ protected function _insertRecords () {
95
+ $ this ->_createTables ();
91
96
92
97
$ data = ['id ' => '1 ' , 'name ' => 'Chuck Norris ' ];
93
98
$ result = $ this ->connection ->insert ('authors ' , $ data , ['id ' => 'integer ' , 'name ' => 'string ' ]);
@@ -96,6 +101,19 @@ protected function _insertTwoRecords() {
96
101
$ result ->bindValue (2 , 'Bruce Lee ' );
97
102
$ result ->execute ();
98
103
104
+ $ data = ['id ' => '2 ' , 'title ' => 'a publication ' , 'body ' => 'a body ' , 'author_id ' => 1 ];
105
+ $ result = $ this ->connection ->insert (
106
+ 'publications ' ,
107
+ $ data ,
108
+ ['id ' => 'integer ' , 'title ' => 'string ' , 'body ' => 'string ' , 'author_id ' => 'integer ' ]
109
+ );
110
+
111
+ $ result ->bindValue (1 , 3 , 'integer ' );
112
+ $ result ->bindValue (2 , 'another publication ' );
113
+ $ result ->bindValue (3 , 'another body ' );
114
+ $ result ->bindValue (4 , 2 );
115
+ $ result ->execute ();
116
+
99
117
$ data = ['id ' => '1 ' , 'title ' => 'a title ' , 'body ' => 'a body ' , 'author_id ' => 1 ];
100
118
$ result = $ this ->connection ->insert (
101
119
'articles ' ,
@@ -272,7 +290,7 @@ public function testContainToFieldsDefault() {
272
290
* @return void
273
291
**/
274
292
public function testContainResultFetchingOneLevel () {
275
- $ this ->_insertTwoRecords ();
293
+ $ this ->_insertRecords ();
276
294
277
295
$ query = new Query ($ this ->connection );
278
296
$ table = Table::build ('article ' , ['table ' => 'articles ' ]);
@@ -310,7 +328,7 @@ public function testContainResultFetchingOneLevel() {
310
328
* @return void
311
329
**/
312
330
public function testHasManyEagerLoading () {
313
- $ this ->_insertTwoRecords ();
331
+ $ this ->_insertRecords ();
314
332
315
333
$ query = new Query ($ this ->connection );
316
334
$ table = Table::build ('author ' , ['connection ' => $ this ->connection ]);
@@ -361,7 +379,7 @@ public function testHasManyEagerLoading() {
361
379
* @return void
362
380
**/
363
381
public function testHasManyEagerLoadingFields () {
364
- $ this ->_insertTwoRecords ();
382
+ $ this ->_insertRecords ();
365
383
366
384
$ query = new Query ($ this ->connection );
367
385
$ table = Table::build ('author ' , ['connection ' => $ this ->connection ]);
@@ -397,7 +415,7 @@ public function testHasManyEagerLoadingFields() {
397
415
* @return void
398
416
**/
399
417
public function testHasManyEagerLoadingOrder () {
400
- $ statement = $ this ->_insertTwoRecords ();
418
+ $ statement = $ this ->_insertRecords ();
401
419
$ statement ->bindValue (1 , 3 , 'integer ' );
402
420
$ statement ->bindValue (2 , 'a fine title ' );
403
421
$ statement ->bindValue (3 , 'a fine body ' );
@@ -439,12 +457,12 @@ public function testHasManyEagerLoadingOrder() {
439
457
}
440
458
441
459
/**
442
- * Tests that deep associations can be eagerly laoded
460
+ * Tests that deep associations can be eagerly loaded
443
461
*
444
462
* @return void
445
463
**/
446
464
public function testHasManyEagerLoadingDeep () {
447
- $ this ->_insertTwoRecords ();
465
+ $ this ->_insertRecords ();
448
466
449
467
$ query = new Query ($ this ->connection );
450
468
$ table = Table::build ('author ' , ['connection ' => $ this ->connection ]);
@@ -483,4 +501,60 @@ public function testHasManyEagerLoadingDeep() {
483
501
$ this ->assertEquals ($ expected , $ results );
484
502
}
485
503
504
+ /**
505
+ * Tests that hasMany associations can be loaded even when related to a secondary
506
+ * model in the query
507
+ *
508
+ * @return void
509
+ **/
510
+ public function testHasManyEagerLoadingFromSecondaryTable () {
511
+ $ this ->_insertRecords ();
512
+
513
+ $ query = new Query ($ this ->connection );
514
+ $ author = Table::build ('author ' , ['connection ' => $ this ->connection ]);
515
+ $ article = Table::build ('article ' , ['connection ' => $ this ->connection ]);
516
+ $ publication = Table::build ('publication ' , ['connection ' => $ this ->connection ]);
517
+
518
+ $ author ->hasMany ('publication ' , ['property ' => 'publications ' ]);
519
+ $ article ->belongsTo ('author ' );
520
+
521
+ $ results = $ query ->repository ($ article )
522
+ ->select ()
523
+ ->contain (['author ' => ['publication ' ]])
524
+ ->toArray ();
525
+ $ expected = [
526
+ [
527
+ 'id ' => 1 ,
528
+ 'title ' => 'a title ' ,
529
+ 'body ' => 'a body ' ,
530
+ 'author_id ' => 1 ,
531
+ 'author ' => [
532
+ 'id ' => 1 , 'name ' => 'Chuck Norris ' ,
533
+ 'publications ' => [
534
+ [
535
+ 'id ' => '2 ' , 'title ' => 'a publication ' ,
536
+ 'body ' => 'a body ' , 'author_id ' => 1
537
+ ]
538
+ ]
539
+ ]
540
+ ],
541
+ [
542
+ 'id ' => 2 ,
543
+ 'title ' => 'another title ' ,
544
+ 'body ' => 'another body ' ,
545
+ 'author_id ' => 2 ,
546
+ 'author ' => [
547
+ 'id ' => 2 , 'name ' => 'Bruce Lee ' ,
548
+ 'publications ' => [
549
+ [
550
+ 'id ' => 3 , 'title ' => 'another publication ' ,
551
+ 'body ' => 'another body ' , 'author_id ' => 2
552
+ ]
553
+ ]
554
+ ]
555
+ ]
556
+ ];
557
+ $ this ->assertEquals ($ expected , $ results );
558
+ }
559
+
486
560
}
0 commit comments