1: <?php namespace Laravel\Database\Eloquent;
2:
3: use Laravel\Event;
4: use Laravel\Database;
5: use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
6:
7: class Query {
8:
9: /**
10: * The model instance being queried.
11: *
12: * @var Model
13: */
14: public $model;
15:
16: /**
17: * The fluent query builder for the query instance.
18: *
19: * @var Query
20: */
21: public $table;
22:
23: /**
24: * The relationships that should be eagerly loaded by the query.
25: *
26: * @var array
27: */
28: public $includes = array();
29:
30: /**
31: * The methods that should be returned from the fluent query builder.
32: *
33: * @var array
34: */
35: public $passthru = array(
36: 'lists', 'only', 'insert', 'insert_get_id', 'update', 'increment',
37: 'delete', 'decrement', 'count', 'min', 'max', 'avg', 'sum',
38: );
39:
40: /**
41: * Creat a new query instance for a model.
42: *
43: * @param Model $model
44: * @return void
45: */
46: public function __construct($model)
47: {
48: $this->model = ($model instanceof Model) ? $model : new $model;
49:
50: $this->table = $this->table();
51: }
52:
53: /**
54: * Find a model by its primary key.
55: *
56: * @param mixed $id
57: * @param array $columns
58: * @return mixed
59: */
60: public function find($id, $columns = array('*'))
61: {
62: $model = $this->model;
63:
64: $this->table->where($model::$key, '=', $id);
65:
66: return $this->first($columns);
67: }
68:
69: /**
70: * Get the first model result for the query.
71: *
72: * @param array $columns
73: * @return mixed
74: */
75: public function first($columns = array('*'))
76: {
77: $results = $this->hydrate($this->model, $this->table->take(1)->get($columns));
78:
79: return (count($results) > 0) ? head($results) : null;
80: }
81:
82: /**
83: * Get all of the model results for the query.
84: *
85: * @param array $columns
86: * @return array
87: */
88: public function get($columns = array('*'))
89: {
90: return $this->hydrate($this->model, $this->table->get($columns));
91: }
92:
93: /**
94: * Get an array of paginated model results.
95: *
96: * @param int $per_page
97: * @param array $columns
98: * @return Paginator
99: */
100: public function paginate($per_page = null, $columns = array('*'))
101: {
102: $per_page = $per_page ?: $this->model->per_page();
103:
104: // First we'll grab the Paginator instance and get the results. Then we can
105: // feed those raw database results into the hydrate method to get models
106: // for the results, which we'll set on the paginator and return it.
107: $paginator = $this->table->paginate($per_page, $columns);
108:
109: $paginator->results = $this->hydrate($this->model, $paginator->results);
110:
111: return $paginator;
112: }
113:
114: /**
115: * Hydrate an array of models from the given results.
116: *
117: * @param Model $model
118: * @param array $results
119: * @return array
120: */
121: public function hydrate($model, $results)
122: {
123: $class = get_class($model);
124:
125: $models = array();
126:
127: // We'll spin through the array of database results and hydrate a model
128: // for each one of the records. We will also set the "exists" flag to
129: // "true" so that the model will be updated when it is saved.
130: foreach ((array) $results as $result)
131: {
132: $result = (array) $result;
133:
134: $new = new $class(array(), true);
135:
136: // We need to set the attributes manually in case the accessible property is
137: // set on the array which will prevent the mass assignemnt of attributes if
138: // we were to pass them in using the constructor or fill methods.
139: $new->fill_raw($result);
140:
141: $models[] = $new;
142: }
143:
144: if (count($results) > 0)
145: {
146: foreach ($this->model_includes() as $relationship => $constraints)
147: {
148: // If the relationship is nested, we will skip loading it here and let
149: // the load method parse and set the nested eager loads on the right
150: // relationship when it is getting ready to eager load.
151: if (str_contains($relationship, '.'))
152: {
153: continue;
154: }
155:
156: $this->load($models, $relationship, $constraints);
157: }
158: }
159:
160: // The many to many relationships may have pivot table column on them
161: // so we will call the "clean" method on the relationship to remove
162: // any pivot columns that are on the model.
163: if ($this instanceof Relationships\Has_Many_And_Belongs_To)
164: {
165: $this->hydrate_pivot($models);
166: }
167:
168: return $models;
169: }
170:
171: /**
172: * Hydrate an eagerly loaded relationship on the model results.
173: *
174: * @param array $results
175: * @param string $relationship
176: * @param array|null $constraints
177: * @return void
178: */
179: protected function load(&$results, $relationship, $constraints)
180: {
181: $query = $this->model->$relationship();
182:
183: $query->model->includes = $this->nested_includes($relationship);
184:
185: // We'll remove any of the where clauses from the relationship to give
186: // the relationship the opportunity to set the constraints for an
187: // eager relationship using a separate, specific method.
188: $query->table->reset_where();
189:
190: $query->eagerly_constrain($results);
191:
192: // Constraints may be specified in-line for the eager load by passing
193: // a Closure as the value portion of the eager load. We can use the
194: // query builder's nested query support to add the constraints.
195: if ( ! is_null($constraints))
196: {
197: $query->table->where_nested($constraints);
198: }
199:
200: $query->initialize($results, $relationship);
201:
202: $query->match($relationship, $results, $query->get());
203: }
204:
205: /**
206: * Gather the nested includes for a given relationship.
207: *
208: * @param string $relationship
209: * @return array
210: */
211: protected function nested_includes($relationship)
212: {
213: $nested = array();
214:
215: foreach ($this->model_includes() as $include => $constraints)
216: {
217: // To get the nested includes, we want to find any includes that begin
218: // the relationship and a dot, then we will strip off the leading
219: // nesting indicator and set the include in the array.
220: if (starts_with($include, $relationship.'.'))
221: {
222: $nested[substr($include, strlen($relationship.'.'))] = $constraints;
223: }
224: }
225:
226: return $nested;
227: }
228:
229: /**
230: * Get the eagerly loaded relationships for the model.
231: *
232: * @return array
233: */
234: protected function model_includes()
235: {
236: $includes = array();
237:
238: foreach ($this->model->includes as $relationship => $constraints)
239: {
240: // When eager loading relationships, constraints may be set on the eager
241: // load definition; however, is none are set, we need to swap the key
242: // and the value of the array since there are no constraints.
243: if (is_numeric($relationship))
244: {
245: list($relationship, $constraints) = array($constraints, null);
246: }
247:
248: $includes[$relationship] = $constraints;
249: }
250:
251: return $includes;
252: }
253:
254: /**
255: * Get a fluent query builder for the model.
256: *
257: * @return Query
258: */
259: protected function table()
260: {
261: return $this->connection()->table($this->model->table());
262: }
263:
264: /**
265: * Get the database connection for the model.
266: *
267: * @return Connection
268: */
269: public function connection()
270: {
271: return Database::connection($this->model->connection());
272: }
273:
274: /**
275: * Handle dynamic method calls to the query.
276: *
277: * @param string $method
278: * @param array $parameters
279: * @return mixed
280: */
281: public function __call($method, $parameters)
282: {
283: $result = call_user_func_array(array($this->table, $method), $parameters);
284:
285: // Some methods may get their results straight from the fluent query
286: // builder such as the aggregate methods. If the called method is
287: // one of these, we will just return the result straight away.
288: if (in_array($method, $this->passthru))
289: {
290: return $result;
291: }
292:
293: return $this;
294: }
295:
296: }