Skip to content

Commit 9718d5c

Browse files
committed
fix nested queries.
1 parent 97761f0 commit 9718d5c

File tree

3 files changed

+56
-37
lines changed

3 files changed

+56
-37
lines changed

laravel/database/eloquent/query.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Query {
3838
);
3939

4040
/**
41-
* Create a new query instance for a model.
41+
* Creat a new query instance for a model.
4242
*
4343
* @param Model $model
4444
* @return void
@@ -118,7 +118,7 @@ public function hydrate($model, $results)
118118
$new = new $class(array(), true);
119119

120120
// We need to set the attributes manually in case the accessible property is
121-
// set on the array which will prevent the mass assignment of attributes if
121+
// set on the array which will prevent the mass assignemnt of attributes if
122122
// we were to pass them in using the constructor or fill methods.
123123
$new->fill_raw($result);
124124

@@ -141,7 +141,7 @@ public function hydrate($model, $results)
141141
}
142142
}
143143

144-
// The many to many relationships may have pivot table columns on them
144+
// The many to many relationships may have pivot table column on them
145145
// so we will call the "clean" method on the relationship to remove
146146
// any pivot columns that are on the model.
147147
if ($this instanceof Relationships\Has_Many_And_Belongs_To)
@@ -199,7 +199,7 @@ protected function nested_includes($relationship)
199199
foreach ($this->model_includes() as $include => $constraints)
200200
{
201201
// To get the nested includes, we want to find any includes that begin
202-
// the relationship with a dot, then we will strip off the leading
202+
// the relationship and a dot, then we will strip off the leading
203203
// nesting indicator and set the include in the array.
204204
if (starts_with($include, $relationship.'.'))
205205
{
@@ -217,23 +217,22 @@ protected function nested_includes($relationship)
217217
*/
218218
protected function model_includes()
219219
{
220-
$relationships = array_keys($this->model->includes);
221-
$implicits = array();
220+
$includes = array();
222221

223-
foreach ($relationships as $relationship)
222+
foreach ($this->model->includes as $relationship => $constraints)
224223
{
225-
$parts = explode('.', $relationship);
226-
227-
$prefix = '';
228-
foreach ($parts as $part)
224+
// When eager loading relationships, constraints may be set on the eager
225+
// load definition; however, is none are set, we need to swap the key
226+
// and the value of the array since there are no constraints.
227+
if (is_numeric($relationship))
229228
{
230-
$implicits[$prefix.$part] = NULL;
231-
$prefix .= $part.'.';
229+
list($relationship, $constraints) = array($constraints, null);
232230
}
231+
232+
$includes[$relationship] = $constraints;
233233
}
234234

235-
// Add all implicit includes to the explicit ones
236-
return $this->model->includes + $implicits;
235+
return $includes;
237236
}
238237

239238
/**
@@ -278,4 +277,4 @@ public function __call($method, $parameters)
278277
return $this;
279278
}
280279

281-
}
280+
}

laravel/database/query.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Closure;
44
use Laravel\Database;
5-
use Paginator;
5+
use Laravel\Paginator;
66
use Laravel\Database\Query\Grammars\Postgres;
77
use Laravel\Database\Query\Grammars\SQLServer;
88

@@ -140,7 +140,7 @@ public function distinct()
140140
*/
141141
public function select($columns = array('*'))
142142
{
143-
$this->selects = is_array($columns) ? $columns : array($columns);
143+
$this->selects = (array) $columns;
144144
return $this;
145145
}
146146

@@ -158,7 +158,7 @@ public function join($table, $column1, $operator = null, $column2 = null, $type
158158
{
159159
// If the "column" is really an instance of a Closure, the developer is
160160
// trying to create a join with a complex "ON" clause. So, we will add
161-
// the join, and then call the Closure with the join.
161+
// the join, and then call the Closure with the join/
162162
if ($column1 instanceof Closure)
163163
{
164164
$this->joins[] = new Query\Join($type, $table);
@@ -168,7 +168,7 @@ public function join($table, $column1, $operator = null, $column2 = null, $type
168168

169169
// If the column is just a string, we can assume that the join just
170170
// has a simple on clause, and we'll create the join instance and
171-
// add the clause automatically for the developer.
171+
// add the clause automatically for the develoepr.
172172
else
173173
{
174174
$join = new Query\Join($type, $table);
@@ -283,7 +283,7 @@ public function or_where($column, $operator = null, $value = null)
283283
*/
284284
public function or_where_id($value)
285285
{
286-
return $this->or_where('id', '=', $value);
286+
return $this->or_where('id', '=', $value);
287287
}
288288

289289
/**
@@ -395,15 +395,32 @@ public function or_where_not_null($column)
395395
}
396396

397397
/**
398-
* Add nested constraints to the query.
398+
* Add a nested where condition to the query.
399399
*
400400
* @param Closure $callback
401401
* @param string $connector
402402
* @return Query
403403
*/
404404
public function where_nested($callback, $connector = 'AND')
405405
{
406-
call_user_func($callback, $this);
406+
$type = 'where_nested';
407+
408+
// To handle a nested where statement, we will actually instantiate a new
409+
// Query instance and run the callback over that instance, which will
410+
// allow the developer to have a fresh query instance
411+
$query = new Query($this->connection, $this->grammar, $this->from);
412+
413+
call_user_func($callback, $query);
414+
415+
// Once the callback has been run on the query, we will store the nested
416+
// query instance on the where clause array so that it's passed to the
417+
// query's query grammar instance when building.
418+
if ($query->wheres !== null)
419+
{
420+
$this->wheres[] = compact('type', 'query', 'connector');
421+
}
422+
423+
$this->bindings = array_merge($this->bindings, $query->bindings);
407424

408425
return $this;
409426
}
@@ -436,7 +453,7 @@ private function dynamic_where($method, $parameters)
436453

437454
foreach ($segments as $segment)
438455
{
439-
// If the segment is not a boolean connector, we can assume it is
456+
// If the segment is not a boolean connector, we can assume it it is
440457
// a column name, and we'll add it to the query as a new constraint
441458
// of the query's where clause and keep iterating the segments.
442459
if ($segment != '_and_' and $segment != '_or_')
@@ -475,7 +492,6 @@ public function group_by($column)
475492
* @param string $column
476493
* @param string $operator
477494
* @param mixed $value
478-
* @return Query
479495
*/
480496
public function having($column, $operator, $value)
481497
{
@@ -660,7 +676,7 @@ public function get($columns = array('*'))
660676
public function aggregate($aggregator, $columns)
661677
{
662678
// We'll set the aggregate value so the grammar does not try to compile
663-
// a SELECT clause on the query. If an aggregator is present, its own
679+
// a SELECT clause on the query. If an aggregator is present, it's own
664680
// grammar function will be used to build the SQL syntax.
665681
$this->aggregate = compact('aggregator', 'columns');
666682

@@ -687,7 +703,7 @@ public function paginate($per_page = 20, $columns = array('*'))
687703
{
688704
// Because some database engines may throw errors if we leave orderings
689705
// on the query when retrieving the total number of records, we'll drop
690-
// all of the orderings and put them back on the query.
706+
// all of the ordreings and put them back on the query.
691707
list($orderings, $this->orderings) = array($this->orderings, null);
692708

693709
$total = $this->count(reset($columns));
@@ -714,12 +730,12 @@ public function insert($values)
714730
{
715731
// Force every insert to be treated like a batch insert to make creating
716732
// the binding array simpler since we can just spin through the inserted
717-
// rows as if there was more than one every time.
733+
// rows as if there/ was more than one every time.
718734
if ( ! is_array(reset($values))) $values = array($values);
719735

720736
$bindings = array();
721737

722-
// We need to merge the insert values into the array of the query
738+
// We need to merge the the insert values into the array of the query
723739
// bindings so that they will be bound to the PDO statement when it
724740
// is executed by the database connection.
725741
foreach ($values as $value)
@@ -820,7 +836,7 @@ public function update($values)
820836
/**
821837
* Execute the query as a DELETE statement.
822838
*
823-
* Optionally, an ID may be passed to the method to delete a specific row.
839+
* Optionally, an ID may be passed to the method do delete a specific row.
824840
*
825841
* @param int $id
826842
* @return int
@@ -837,7 +853,7 @@ public function delete($id = null)
837853

838854
$sql = $this->grammar->delete($this);
839855

840-
return $this->connection->query($sql, $this->bindings);
856+
return $this->connection->query($sql, $this->bindings);
841857
}
842858

843859
/**
@@ -853,7 +869,7 @@ public function __call($method, $parameters)
853869
}
854870

855871
// All of the aggregate methods are handled by a single method, so we'll
856-
// catch them all here and then pass them off to the aggregate method
872+
// catch them all here and then pass them off to the agregate method
857873
// instead of creating methods for each one of them.
858874
if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
859875
{

laravel/documentation/changes.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## Contents
44

5-
- [Develop](#develop)
5+
- [Laravel 3.2.5](#3.2.5)
6+
- [Upgrading From 3.2.4](#upgrade-3.2.5)
67
- [Laravel 3.2.4](#3.2.4)
78
- [Upgrading From 3.2.3](#upgrade-3.2.4)
89
- [Laravel 3.2.3](#3.2.3)
@@ -34,11 +35,14 @@
3435
- [Laravel 3.1](#3.1)
3536
- [Upgrading From 3.0](#upgrade-3.1)
3637

37-
<a name="develop"></a>
38-
## Develop
38+
<a name="3.2.5"></a>
3939

40-
- Added Turkish language files.
41-
- Changed jQuery '$' to 'jQuery' in the Profiler.
40+
- Revert nested where code back to 3.2.3 tag.
41+
42+
<a name="upgrade-3.2.5"></a>
43+
## Upgrading From 3.2.4
44+
45+
- Replace the **laravel** folder.
4246

4347
<a name="3.2.4"></a>
4448
## Laravel 3.2.4

0 commit comments

Comments
 (0)