@@ -195,7 +195,7 @@ public function normalized(Table $repository)
195
195
$ contain = (array )$ this ->_containments ;
196
196
break ;
197
197
}
198
- $ contain [$ alias ] =& $ this ->_normalizeContain (
198
+ $ contain [$ alias ] = $ this ->_normalizeContain (
199
199
$ repository ,
200
200
$ alias ,
201
201
$ options ,
@@ -279,13 +279,13 @@ public function attachAssociations(Query $query, Table $repository, $includeFiel
279
279
return ;
280
280
}
281
281
282
- foreach ($ this ->attachableAssociations ($ repository ) as $ options ) {
283
- $ config = $ options [ ' config ' ] + [
284
- 'aliasPath ' => $ options [ ' aliasPath ' ] ,
285
- 'propertyPath ' => $ options [ ' propertyPath ' ] ,
282
+ foreach ($ this ->attachableAssociations ($ repository ) as $ loadable ) {
283
+ $ config = $ loadable -> config () + [
284
+ 'aliasPath ' => $ loadable -> aliasPath () ,
285
+ 'propertyPath ' => $ loadable -> propertyPath () ,
286
286
'includeFields ' => $ includeFields
287
287
];
288
- $ options [ ' instance ' ] ->attachTo ($ query , $ config );
288
+ $ loadable -> instance () ->attachTo ($ query , $ config );
289
289
}
290
290
}
291
291
@@ -344,7 +344,7 @@ public function externalAssociations(Table $repository)
344
344
* @return array normalized associations
345
345
* @throws \InvalidArgumentException When containments refer to associations that do not exist.
346
346
*/
347
- protected function & _normalizeContain (Table $ parent , $ alias , $ options , $ paths )
347
+ protected function _normalizeContain (Table $ parent , $ alias , $ options , $ paths )
348
348
{
349
349
$ defaults = $ this ->_containOptions ;
350
350
$ instance = $ parent ->association ($ alias );
@@ -369,18 +369,22 @@ protected function &_normalizeContain(Table $parent, $alias, $options, $paths)
369
369
'propertyPath ' => trim ($ paths ['propertyPath ' ], '. ' )
370
370
];
371
371
$ config ['canBeJoined ' ] = $ instance ->canBeJoined ($ config ['config ' ]);
372
+ $ eagerLoadable = new EagerLoadable ($ alias , $ config );
372
373
373
374
if ($ config ['canBeJoined ' ]) {
374
- $ this ->_aliasList [$ paths ['root ' ]][$ alias ][] =& $ config ;
375
+ $ this ->_aliasList [$ paths ['root ' ]][$ alias ][] = $ eagerLoadable ;
375
376
} else {
376
377
$ paths ['root ' ] = $ config ['aliasPath ' ];
377
378
}
378
379
379
380
foreach ($ extra as $ t => $ assoc ) {
380
- $ config ['associations ' ][$ t ] =& $ this ->_normalizeContain ($ table , $ t , $ assoc , $ paths );
381
+ $ eagerLoadable ->addAssociation (
382
+ $ t ,
383
+ $ this ->_normalizeContain ($ table , $ t , $ assoc , $ paths )
384
+ );
381
385
}
382
386
383
- return $ config ;
387
+ return $ eagerLoadable ;
384
388
}
385
389
386
390
/**
@@ -394,14 +398,14 @@ protected function &_normalizeContain(Table $parent, $alias, $options, $paths)
394
398
*/
395
399
protected function _fixStrategies ()
396
400
{
397
- foreach ($ this ->_aliasList as & $ aliases ) {
398
- foreach ($ aliases as $ alias => & $ configs ) {
401
+ foreach ($ this ->_aliasList as $ aliases ) {
402
+ foreach ($ aliases as $ alias => $ configs ) {
399
403
if (count ($ configs ) < 2 ) {
400
404
continue ;
401
405
}
402
- foreach ($ configs as & $ config ) {
403
- if (strpos ($ config [ ' aliasPath ' ] , '. ' )) {
404
- $ this ->_correctStrategy ($ config , $ alias );
406
+ foreach ($ configs as $ loadable ) {
407
+ if (strpos ($ loadable -> aliasPath () , '. ' )) {
408
+ $ this ->_correctStrategy ($ loadable , $ alias );
405
409
}
406
410
}
407
411
}
@@ -412,26 +416,23 @@ protected function _fixStrategies()
412
416
* Changes the association fetching strategy if required because of duplicate
413
417
* under the same direct associations chain
414
418
*
415
- * This function modifies the $config variable
416
- *
417
- * @param array &$config The association config
419
+ * @param \Cake\ORM\EagerLoader $loadable The association config
418
420
* @param string $alias the name of the association to evaluate
419
- * @return void|array
420
- * @throws \RuntimeException if a duplicate association in the same chain is detected
421
- * but is not possible to change the strategy due to conflicting settings
421
+ * @return void
422
422
*/
423
- protected function _correctStrategy (& $ config , $ alias )
423
+ protected function _correctStrategy ($ loadable , $ alias )
424
424
{
425
- $ currentStrategy = isset ($ config ['config ' ]['strategy ' ]) ?
426
- $ config ['config ' ]['strategy ' ] :
425
+ $ config = $ loadable ->config ();
426
+ $ currentStrategy = isset ($ config ['strategy ' ]) ?
427
+ $ config ['strategy ' ] :
427
428
'join ' ;
428
429
429
- if (!$ config [ ' canBeJoined ' ] || $ currentStrategy !== 'join ' ) {
430
- return $ config ;
430
+ if (!$ loadable -> canBeJoined () || $ currentStrategy !== 'join ' ) {
431
+ return ;
431
432
}
432
433
433
- $ config[ ' canBeJoined ' ] = false ;
434
- $ config [ ' config ' ][ ' strategy ' ] = $ config [ ' instance ' ]:: STRATEGY_SELECT ;
434
+ $ config = $ config [ ' strategy ' ] = Association:: STRATEGY_SELECT ;
435
+ $ loadable -> canBeJoined ( false ) ;
435
436
}
436
437
437
438
/**
@@ -447,15 +448,18 @@ protected function _resolveJoins($associations, $matching = [])
447
448
$ result = [];
448
449
foreach ($ matching as $ table => $ options ) {
449
450
$ result [$ table ] = $ options ;
450
- $ result += $ this ->_resolveJoins ($ options[ ' associations ' ] , []);
451
+ $ result += $ this ->_resolveJoins ($ options-> associations () , []);
451
452
}
452
453
foreach ($ associations as $ table => $ options ) {
453
454
$ inMatching = isset ($ matching [$ table ]);
454
- if (!$ inMatching && $ options[ ' canBeJoined ' ] ) {
455
+ if (!$ inMatching && $ options-> canBeJoined () ) {
455
456
$ result [$ table ] = $ options ;
456
- $ result += $ this ->_resolveJoins ($ options ['associations ' ], $ inMatching ? $ mathching [$ table ] : []);
457
+ $ result += $ this ->_resolveJoins (
458
+ $ options ->associations (),
459
+ $ inMatching ? $ mathching [$ table ] : []
460
+ );
457
461
} else {
458
- $ options[ ' canBeJoined ' ] = false ;
462
+ $ options-> canBeJoined ( false ) ;
459
463
$ this ->_loadExternal [] = $ options ;
460
464
}
461
465
}
@@ -481,21 +485,23 @@ public function loadExternal($query, $statement)
481
485
$ driver = $ query ->connection ()->driver ();
482
486
list ($ collected , $ statement ) = $ this ->_collectKeys ($ external , $ query , $ statement );
483
487
foreach ($ external as $ meta ) {
484
- $ contain = $ meta ['associations ' ];
485
- $ alias = $ meta ['instance ' ]->source ()->alias ();
488
+ $ contain = $ meta ->associations ();
489
+ $ instance = $ meta ->instance ();
490
+ $ config = $ meta ->config ();
491
+ $ alias = $ instance ->source ()->alias ();
486
492
487
- $ requiresKeys = $ meta [ ' instance ' ] ->requiresKeys ($ meta [ ' config ' ] );
493
+ $ requiresKeys = $ instance ->requiresKeys ($ config );
488
494
if ($ requiresKeys && empty ($ collected [$ alias ])) {
489
495
continue ;
490
496
}
491
497
492
498
$ keys = isset ($ collected [$ alias ]) ? $ collected [$ alias ] : null ;
493
- $ f = $ meta [ ' instance ' ] ->eagerLoader (
494
- $ meta [ ' config ' ] + [
499
+ $ f = $ instance ->eagerLoader (
500
+ $ config + [
495
501
'query ' => $ query ,
496
502
'contain ' => $ contain ,
497
503
'keys ' => $ keys ,
498
- 'nestKey ' => $ meta[ ' aliasPath ' ]
504
+ 'nestKey ' => $ meta-> aliasPath ()
499
505
]
500
506
);
501
507
$ statement = new CallbackStatement ($ statement , $ driver , $ f );
@@ -528,16 +534,20 @@ public function associationsMap($table)
528
534
529
535
$ visitor = function ($ level , $ matching = false ) use (&$ visitor , &$ map ) {
530
536
foreach ($ level as $ assoc => $ meta ) {
537
+ $ canBeJoined = $ meta ->canBeJoined ();
538
+ $ instance = $ meta ->instance ();
539
+ $ associations = $ meta ->associations ();
540
+ $ forMatching = $ meta ->forMatching ();
531
541
$ map [] = [
532
542
'alias ' => $ assoc ,
533
- 'instance ' => $ meta [ ' instance ' ] ,
534
- 'canBeJoined ' => $ meta [ ' canBeJoined ' ] ,
535
- 'entityClass ' => $ meta [ ' instance ' ] ->target ()->entityClass (),
536
- 'nestKey ' => $ meta [ ' canBeJoined ' ] ? $ assoc : $ meta[ ' aliasPath ' ] ,
537
- 'matching ' => isset ( $ meta [ ' matching ' ]) ? $ meta [ ' matching ' ] : $ matching
543
+ 'instance ' => $ instance ,
544
+ 'canBeJoined ' => $ canBeJoined ,
545
+ 'entityClass ' => $ instance ->target ()->entityClass (),
546
+ 'nestKey ' => $ canBeJoined ? $ assoc : $ meta-> aliasPath () ,
547
+ 'matching ' => $ forMatching !== null ? $ forMatching : $ matching
538
548
];
539
- if ($ meta [ ' canBeJoined ' ] && ! empty ( $ meta [ ' associations ' ]) ) {
540
- $ visitor ($ meta [ ' associations ' ] , $ matching );
549
+ if ($ canBeJoined && $ associations ) {
550
+ $ visitor ($ associations , $ matching );
541
551
}
542
552
}
543
553
};
@@ -561,13 +571,12 @@ public function associationsMap($table)
561
571
*/
562
572
public function addToJoinsMap ($ alias , Association $ assoc , $ asMatching = false )
563
573
{
564
- $ this ->_joinsMap [$ alias ] = [
574
+ $ this ->_joinsMap [$ alias ] = new EagerLoadable ( $ alias , [
565
575
'aliasPath ' => $ alias ,
566
576
'instance ' => $ assoc ,
567
577
'canBeJoined ' => true ,
568
- 'matching ' => $ asMatching ,
569
- 'associations ' => []
570
- ];
578
+ 'forMatching ' => $ asMatching ,
579
+ ]);
571
580
}
572
581
573
582
/**
@@ -583,13 +592,14 @@ protected function _collectKeys($external, $query, $statement)
583
592
{
584
593
$ collectKeys = [];
585
594
foreach ($ external as $ meta ) {
586
- if (!$ meta ['instance ' ]->requiresKeys ($ meta ['config ' ])) {
595
+ $ instance = $ meta ->instance ();
596
+ if (!$ instance ->requiresKeys ($ meta ->config ())) {
587
597
continue ;
588
598
}
589
599
590
- $ source = $ meta [ ' instance ' ] ->source ();
591
- $ keys = $ meta [ ' instance ' ] ->type () === $ meta [ ' instance ' ] ::MANY_TO_ONE ?
592
- (array )$ meta [ ' instance ' ] ->foreignKey () :
600
+ $ source = $ instance ->source ();
601
+ $ keys = $ instance ->type () === Association ::MANY_TO_ONE ?
602
+ (array )$ instance ->foreignKey () :
593
603
(array )$ source ->primaryKey ();
594
604
595
605
$ alias = $ source ->alias ();
0 commit comments