query.php 6.8 KB

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