Skip to content

Commit de1bf43

Browse files
committed
Documenting and cleaning up
1 parent e40c150 commit de1bf43

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/ORM/EagerLoader.php

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
use Closure;
2222

2323
/**
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.
2528
*/
2629
class EagerLoader {
2730

@@ -31,7 +34,7 @@ class EagerLoader {
3134
*
3235
* @var array
3336
*/
34-
protected $_containments;
37+
protected $_containments = [];
3538

3639
/**
3740
* Contains a nested array with the compiled containments tree
@@ -58,24 +61,40 @@ class EagerLoader {
5861
];
5962

6063
/**
61-
* A list of associations that should be eagerly loaded
64+
* A list of associations that should be loaded with a separate query
6265
*
6366
* @var array
6467
*/
65-
protected $_loadEagerly = [];
68+
protected $_loadExternal = [];
6669

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-
}
7670

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 = []) {
7796
if (empty($associations)) {
78-
return;
97+
return $this->_containments;
7998
}
8099

81100
$associations = (array)$associations;
@@ -86,8 +105,8 @@ public function contain($associations = null, $override = false) {
86105
}
87106

88107
$associations = $this->_reformatContain($associations, $this->_containments);
89-
$this->_containments = $associations;
90108
$this->_normalized = null;
109+
return $this->_containments = $associations;
91110
}
92111

93112
public function matching($assoc, callable $builder = null) {
@@ -136,7 +155,7 @@ public function normalized(Table $repository) {
136155

137156
public function hasExternal(Table $repository) {
138157
$this->normalized($repository);
139-
return !empty($this->_loadEagerly);
158+
return !empty($this->_loadExternal);
140159
}
141160

142161
/**
@@ -238,7 +257,7 @@ protected function _normalizeContain(Table $parent, $alias, $options) {
238257
}
239258

240259
if (!$config['canBeJoined']) {
241-
$this->_loadEagerly[$alias] = $config;
260+
$this->_loadExternal[$alias] = $config;
242261
}
243262

244263
return $config;
@@ -278,7 +297,7 @@ public function eagerLoad($query, $statement) {
278297

279298
$driver = $query->connection()->driver();
280299
list($collected, $statement) = $this->_collectKeys($query, $statement);
281-
foreach ($this->_loadEagerly as $meta) {
300+
foreach ($this->_loadExternal as $meta) {
282301
$contain = $meta['associations'];
283302
$alias = $meta['instance']->source()->alias();
284303
$keys = isset($collected[$alias]) ? $collected[$alias] : null;
@@ -301,7 +320,7 @@ public function eagerLoad($query, $statement) {
301320
*/
302321
protected function _collectKeys($query, $statement) {
303322
$collectKeys = [];
304-
foreach ($this->_loadEagerly as $meta) {
323+
foreach ($this->_loadExternal as $meta) {
305324
$source = $meta['instance']->source();
306325
if ($meta['instance']->requiresKeys($meta['config'])) {
307326
$alias = $source->alias();

src/ORM/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function contain($associations = null, $override = false) {
302302
$this->_eagerLoader = null;
303303
}
304304

305-
$result = $this->eagerLoader()->contain($associations, $override);
305+
$result = $this->eagerLoader()->contain($associations);
306306
if ($associations !== null || $override) {
307307
$this->_dirty();
308308
}

0 commit comments

Comments
 (0)