21
21
use Closure ;
22
22
23
23
/**
24
- *
24
+ * Exposes the methods for storing the associations that should be eager loaded
25
+ * for a table once a query is provided and delegates the job of creating the
26
+ * required joins and decorating the results so that those associations can be
27
+ * part of the result set.
25
28
*/
26
29
class EagerLoader {
27
30
@@ -31,7 +34,7 @@ class EagerLoader {
31
34
*
32
35
* @var array
33
36
*/
34
- protected $ _containments ;
37
+ protected $ _containments = [] ;
35
38
36
39
/**
37
40
* Contains a nested array with the compiled containments tree
@@ -58,24 +61,40 @@ class EagerLoader {
58
61
];
59
62
60
63
/**
61
- * A list of associations that should be eagerly loaded
64
+ * A list of associations that should be loaded with a separate query
62
65
*
63
66
* @var array
64
67
*/
65
- protected $ _loadEagerly = [];
68
+ protected $ _loadExternal = [];
66
69
67
- public function contain ($ associations = null , $ override = false ) {
68
- if ($ this ->_containments === null || $ override ) {
69
- $ this ->_containments = [];
70
- $ this ->_normalized = null ;
71
- }
72
-
73
- if ($ associations === null ) {
74
- return $ this ->_containments ;
75
- }
76
70
71
+ /**
72
+ * Sets the list of associations that should be eagerly loaded along for a
73
+ * specific table using when a query is provided. The list of associated tables
74
+ * passed to this method must have been previously set as associations using the
75
+ * Table API.
76
+ *
77
+ * Associations can be arbitrarily nested using dot notation or nested arrays,
78
+ * this allows this object to calculate joins or any additional queries that
79
+ * must be executed to bring the required associated data.
80
+ *
81
+ * Accepted options per passed association:
82
+ *
83
+ * - foreignKey: Used to set a different field to match both tables, if set to false
84
+ * no join conditions will be generated automatically
85
+ * - fields: An array with the fields that should be fetched from the association
86
+ * - queryBuilder: Equivalent to passing a callable instead of an options array
87
+ * - matching: Whether to inform the association class that it should filter the
88
+ * main query by the results fetched by that class.
89
+ *
90
+ * @param array|string $associations list of table aliases to be queried.
91
+ * When this method is called multiple times it will merge previous list with
92
+ * the new one.
93
+ * @return array
94
+ */
95
+ public function contain ($ associations = []) {
77
96
if (empty ($ associations )) {
78
- return ;
97
+ return $ this -> _containments ;
79
98
}
80
99
81
100
$ associations = (array )$ associations ;
@@ -86,8 +105,8 @@ public function contain($associations = null, $override = false) {
86
105
}
87
106
88
107
$ associations = $ this ->_reformatContain ($ associations , $ this ->_containments );
89
- $ this ->_containments = $ associations ;
90
108
$ this ->_normalized = null ;
109
+ return $ this ->_containments = $ associations ;
91
110
}
92
111
93
112
public function matching ($ assoc , callable $ builder = null ) {
@@ -136,7 +155,7 @@ public function normalized(Table $repository) {
136
155
137
156
public function hasExternal (Table $ repository ) {
138
157
$ this ->normalized ($ repository );
139
- return !empty ($ this ->_loadEagerly );
158
+ return !empty ($ this ->_loadExternal );
140
159
}
141
160
142
161
/**
@@ -238,7 +257,7 @@ protected function _normalizeContain(Table $parent, $alias, $options) {
238
257
}
239
258
240
259
if (!$ config ['canBeJoined ' ]) {
241
- $ this ->_loadEagerly [$ alias ] = $ config ;
260
+ $ this ->_loadExternal [$ alias ] = $ config ;
242
261
}
243
262
244
263
return $ config ;
@@ -278,7 +297,7 @@ public function eagerLoad($query, $statement) {
278
297
279
298
$ driver = $ query ->connection ()->driver ();
280
299
list ($ collected , $ statement ) = $ this ->_collectKeys ($ query , $ statement );
281
- foreach ($ this ->_loadEagerly as $ meta ) {
300
+ foreach ($ this ->_loadExternal as $ meta ) {
282
301
$ contain = $ meta ['associations ' ];
283
302
$ alias = $ meta ['instance ' ]->source ()->alias ();
284
303
$ keys = isset ($ collected [$ alias ]) ? $ collected [$ alias ] : null ;
@@ -301,7 +320,7 @@ public function eagerLoad($query, $statement) {
301
320
*/
302
321
protected function _collectKeys ($ query , $ statement ) {
303
322
$ collectKeys = [];
304
- foreach ($ this ->_loadEagerly as $ meta ) {
323
+ foreach ($ this ->_loadExternal as $ meta ) {
305
324
$ source = $ meta ['instance ' ]->source ();
306
325
if ($ meta ['instance ' ]->requiresKeys ($ meta ['config ' ])) {
307
326
$ alias = $ source ->alias ();
0 commit comments