Merge pull request #946 from franzliedke/patch-23
Fix eager loading constraints
This commit is contained in:
commit
5c3ede74d9
|
@ -257,26 +257,21 @@ public function _with($includes)
|
||||||
{
|
{
|
||||||
$includes = (array) $includes;
|
$includes = (array) $includes;
|
||||||
|
|
||||||
$all_includes = array();
|
$this->includes = array();
|
||||||
|
|
||||||
foreach($includes as $include)
|
foreach ($includes as $relationship => $constraints)
|
||||||
{
|
{
|
||||||
$nested = explode('.', $include);
|
// When eager loading relationships, constraints may be set on the eager
|
||||||
|
// load definition; however, is none are set, we need to swap the key
|
||||||
$inc = array();
|
// and the value of the array since there are no constraints.
|
||||||
|
if (is_numeric($relationship))
|
||||||
foreach($nested as $relation)
|
|
||||||
{
|
{
|
||||||
$inc[] = $relation;
|
list($relationship, $constraints) = array($constraints, null);
|
||||||
|
|
||||||
$all_includes[] = implode('.', $inc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->includes[$relationship] = $constraints;
|
||||||
}
|
}
|
||||||
|
|
||||||
//remove duplicates and reset the array keys.
|
|
||||||
$this->includes = array_values(array_unique($all_includes));
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -217,22 +217,23 @@ protected function nested_includes($relationship)
|
||||||
*/
|
*/
|
||||||
protected function model_includes()
|
protected function model_includes()
|
||||||
{
|
{
|
||||||
$includes = array();
|
$relationships = array_keys($this->model->includes);
|
||||||
|
$implicits = array();
|
||||||
|
|
||||||
foreach ($this->model->includes as $relationship => $constraints)
|
foreach ($relationships as $relationship)
|
||||||
{
|
{
|
||||||
// When eager loading relationships, constraints may be set on the eager
|
$parts = explode('.', $relationship);
|
||||||
// load definition; however, is none are set, we need to swap the key
|
|
||||||
// and the value of the array since there are no constraints.
|
$prefix = '';
|
||||||
if (is_numeric($relationship))
|
foreach ($parts as $part)
|
||||||
{
|
{
|
||||||
list($relationship, $constraints) = array($constraints, null);
|
$implicits[$prefix.$part] = NULL;
|
||||||
|
$prefix .= $part.'.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$includes[$relationship] = $constraints;
|
// Add all implicit includes to the explicit ones
|
||||||
}
|
return $this->model->includes + $implicits;
|
||||||
|
|
||||||
return $includes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue