query.php 6.8 KB

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