From 71f8e4acc09d0c768e4f54dfc09b1e15f09ddfeb Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 12 Jul 2012 23:16:22 +0300 Subject: [PATCH] Fix a bug introduced in pull request #799 that caused eager loading constraints to stop working. --- laravel/database/eloquent/model.php | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index ccdd42d5..9a90279f 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -257,25 +257,38 @@ public function _with($includes) { $includes = (array) $includes; - $all_includes = array(); + $given_includes = array(); - foreach($includes as $include) + foreach ($includes as $relationship => $constraints) { - $nested = explode('.', $include); - - $inc = array(); - - foreach($nested as $relation) + // When eager loading relationships, constraints may be set on the eager + // load definition; however, is none are set, we need to swap the key + // and the value of the array since there are no constraints. + if (is_numeric($relationship)) { - $inc[] = $relation; - - $all_includes[] = implode('.', $inc); + list($relationship, $constraints) = array($constraints, null); } + $given_includes[$relationship] = $constraints; } - //remove duplicates and reset the array keys. - $this->includes = array_values(array_unique($all_includes)); + $relationships = array_keys($given_includes); + $implicits = array(); + + foreach ($relationships as $relationship) + { + $parts = explode('.', $relationship); + + $prefix = ''; + foreach ($parts as $part) + { + $implicits[$prefix.$part] = null; + $prefix .= $part.'.'; + } + } + + // Add all implicit includes to the explicit ones + $this->includes = $given_includes + $implicits; return $this; }