From 71f8e4acc09d0c768e4f54dfc09b1e15f09ddfeb Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 12 Jul 2012 23:16:22 +0300 Subject: [PATCH 1/3] 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 @@ abstract class Model { { $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; } From 8c2a4fba14015f166451f25f6c9ef3f87c0fe51a Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 12 Jul 2012 23:20:17 +0300 Subject: [PATCH 2/3] The Eloquent model includes can now be taken directly from the model when assembling the query. --- laravel/database/eloquent/query.php | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/laravel/database/eloquent/query.php b/laravel/database/eloquent/query.php index 3aee79c9..25fa4fbc 100644 --- a/laravel/database/eloquent/query.php +++ b/laravel/database/eloquent/query.php @@ -127,7 +127,7 @@ class Query { if (count($results) > 0) { - foreach ($this->model_includes() as $relationship => $constraints) + foreach ($this->model->includes as $relationship => $constraints) { // If the relationship is nested, we will skip loading it here and let // the load method parse and set the nested eager loads on the right @@ -196,7 +196,7 @@ class Query { { $nested = array(); - foreach ($this->model_includes() as $include => $constraints) + foreach ($this->model->includes as $include => $constraints) { // To get the nested includes, we want to find any includes that begin // the relationship and a dot, then we will strip off the leading @@ -210,31 +210,6 @@ class Query { return $nested; } - /** - * Get the eagerly loaded relationships for the model. - * - * @return array - */ - protected function model_includes() - { - $includes = array(); - - foreach ($this->model->includes as $relationship => $constraints) - { - // 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)) - { - list($relationship, $constraints) = array($constraints, null); - } - - $includes[$relationship] = $constraints; - } - - return $includes; - } - /** * Get a fluent query builder for the model. * From 78920c5d270f0abcf3eccf557fea88d83d3470ed Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 12 Jul 2012 22:32:43 +0200 Subject: [PATCH 3/3] Move automatic relationship parsing to Eloquent's query class. --- laravel/database/eloquent/model.php | 22 ++------------------- laravel/database/eloquent/query.php | 30 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 9a90279f..32c504c5 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -257,7 +257,7 @@ abstract class Model { { $includes = (array) $includes; - $given_includes = array(); + $this->includes = array(); foreach ($includes as $relationship => $constraints) { @@ -269,27 +269,9 @@ abstract class Model { list($relationship, $constraints) = array($constraints, null); } - $given_includes[$relationship] = $constraints; + $this->includes[$relationship] = $constraints; } - $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; } diff --git a/laravel/database/eloquent/query.php b/laravel/database/eloquent/query.php index 25fa4fbc..56fd15c7 100644 --- a/laravel/database/eloquent/query.php +++ b/laravel/database/eloquent/query.php @@ -127,7 +127,7 @@ class Query { if (count($results) > 0) { - foreach ($this->model->includes as $relationship => $constraints) + foreach ($this->model_includes() as $relationship => $constraints) { // If the relationship is nested, we will skip loading it here and let // the load method parse and set the nested eager loads on the right @@ -196,7 +196,7 @@ class Query { { $nested = array(); - foreach ($this->model->includes as $include => $constraints) + foreach ($this->model_includes() as $include => $constraints) { // To get the nested includes, we want to find any includes that begin // the relationship and a dot, then we will strip off the leading @@ -210,6 +210,32 @@ class Query { return $nested; } + /** + * Get the eagerly loaded relationships for the model. + * + * @return array + */ + protected function model_includes() + { + $relationships = array_keys($this->model->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 + return $this->model->includes + $implicits; + } + /** * Get a fluent query builder for the model. *