Browse Source

Fix a bug introduced in pull request #799 that caused eager loading constraints to stop working.

Franz Liedke 12 years ago
parent
commit
71f8e4acc0
1 changed files with 24 additions and 11 deletions
  1. 24 11
      laravel/database/eloquent/model.php

+ 24 - 11
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);
+			// 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);
+			}
 
-			$inc = array();
+			$given_includes[$relationship] = $constraints;
+		}
 
-			foreach($nested as $relation)
-			{
-				$inc[] = $relation;
+		$relationships = array_keys($given_includes);
+		$implicits = array();
 
-				$all_includes[] = implode('.', $inc);
-			}
+		foreach ($relationships as $relationship)
+		{
+			$parts = explode('.', $relationship);
 
+			$prefix = '';
+			foreach ($parts as $part)
+			{
+				$implicits[$prefix.$part] = null;
+				$prefix .= $part.'.';
+			}
 		}
 
-		//remove duplicates and reset the array keys.
-		$this->includes = array_values(array_unique($all_includes));
+		// Add all implicit includes to the explicit ones
+		$this->includes = $given_includes + $implicits;
 
 		return $this;
 	}