Permalink
Browse files

Fix nested wheres, but still allows all types of constraints to be ap…

…plied to eager loads.

Fixes #1065.
  • Loading branch information...
1 parent bc34498 commit 6c3f3820bed463fcf1b6267c68c7a183d9949d96 @franzliedke committed Aug 6, 2012
@@ -171,15 +171,9 @@ protected function load(&$results, $relationship, $constraints)
// eager relationship using a separate, specific method.
$query->table->reset_where();
- $query->eagerly_constrain($results);
-
// Constraints may be specified in-line for the eager load by passing
- // a Closure as the value portion of the eager load. We can use the
- // query builder's nested query support to add the constraints.
- if ( ! is_null($constraints))
- {
- $query->table->where_nested($constraints);
- }
+ // a Closure as the value portion of the eager load.
+ $query->eagerly_constrain($results, $constraints);
$query->initialize($results, $relationship);
@@ -1,6 +1,7 @@
<?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Database\Eloquent\Model;
+use Closure;
class Has_One_Or_Many extends Relationship {
@@ -48,12 +49,18 @@ protected function constrain()
/**
* Set the proper constraints on the relationship table for an eager load.
*
- * @param array $results
+ * @param array $results
+ * @param Closure $constraints
* @return void
*/
- public function eagerly_constrain($results)
+ public function eagerly_constrain($results, Closure $constraints = null)
{
$this->table->where_in($this->foreign_key(), $this->keys($results));
+
+ if ( ! is_null($constraints))
+ {
+ call_user_func($constraints, $this->table);
+ }
}
}
View
@@ -395,15 +395,32 @@ public function or_where_not_null($column)
}
/**
- * Add nested constraints to the query.
+ * Add a nested where condition to the query.
*
* @param Closure $callback
* @param string $connector
* @return Query
*/
public function where_nested($callback, $connector = 'AND')
{
- call_user_func($callback, $this);
+ $type = 'where_nested';
+
+ // To handle a nested where statement, we will actually instantiate a new
+ // Query instance and run the callback over that instance, which will
+ // allow the developer to have a fresh query instance
+ $query = new Query($this->connection, $this->grammar, $this->from);
+
+ call_user_func($callback, $query);
+
+ // Once the callback has been run on the query, we will store the nested
+ // query instance on the where clause array so that it's passed to the
+ // query's query grammar instance when building.
+ if ($query->wheres !== null)
+ {
+ $this->wheres[] = compact('type', 'query', 'connector');
+ }
+
+ $this->bindings = array_merge($this->bindings, $query->bindings);
return $this;
}

0 comments on commit 6c3f382

Please sign in to comment.