query.php 7.1 KB

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