hydrator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php namespace Laravel\Database\Eloquent;
  2. class Hydrator {
  3. /**
  4. * Load the array of hydrated models and their eager relationships.
  5. *
  6. * @param Model $eloquent
  7. * @return array
  8. */
  9. public static function hydrate($eloquent)
  10. {
  11. $results = static::base(get_class($eloquent), $eloquent->query->get());
  12. if (count($results) > 0)
  13. {
  14. foreach ($eloquent->includes as $include)
  15. {
  16. if ( ! method_exists($eloquent, $include))
  17. {
  18. throw new \Exception("Attempting to eager load [$include], but the relationship is not defined.");
  19. }
  20. static::eagerly($eloquent, $results, $include);
  21. }
  22. }
  23. return $results;
  24. }
  25. /**
  26. * Hydrate the base models for a query.
  27. *
  28. * The resulting model array is keyed by the primary keys of the models.
  29. * This allows the models to easily be matched to their children.
  30. *
  31. * @param string $class
  32. * @param array $results
  33. * @return array
  34. */
  35. private static function base($class, $results)
  36. {
  37. $models = array();
  38. foreach ($results as $result)
  39. {
  40. $model = new $class;
  41. $model->attributes = (array) $result;
  42. $model->exists = true;
  43. if (isset($model->attributes['id']))
  44. {
  45. $models[$model->id] = $model;
  46. }
  47. else
  48. {
  49. $models[] = $model;
  50. }
  51. }
  52. return $models;
  53. }
  54. /**
  55. * Eagerly load a relationship.
  56. *
  57. * @param object $eloquent
  58. * @param array $parents
  59. * @param string $include
  60. * @return void
  61. */
  62. private static function eagerly($eloquent, &$parents, $include)
  63. {
  64. // We temporarily spoof the query attributes to allow the query to be fetched without
  65. // any problems, since the belongs_to method actually gets the related attribute.
  66. $first = reset($parents);
  67. $eloquent->attributes = $first->attributes;
  68. $relationship = $eloquent->$include();
  69. $eloquent->attributes = array();
  70. // Reset the WHERE clause and bindings on the query. We'll add our own WHERE clause soon.
  71. // This will allow us to load a range of related models instead of only one.
  72. $relationship->query->reset_where();
  73. // Initialize the relationship attribute on the parents. As expected, "many" relationships
  74. // are initialized to an array and "one" relationships are initialized to null.
  75. foreach ($parents as &$parent)
  76. {
  77. $parent->ignore[$include] = (in_array($eloquent->relating, array('has_many', 'has_and_belongs_to_many'))) ? array() : null;
  78. }
  79. if (in_array($relating = $eloquent->relating, array('has_one', 'has_many', 'belongs_to')))
  80. {
  81. return static::$relating($relationship, $parents, $eloquent->relating_key, $include);
  82. }
  83. else
  84. {
  85. static::has_and_belongs_to_many($relationship, $parents, $eloquent->relating_key, $eloquent->relating_table, $include);
  86. }
  87. }
  88. /**
  89. * Eagerly load a 1:1 relationship.
  90. *
  91. * @param object $relationship
  92. * @param array $parents
  93. * @param string $relating_key
  94. * @param string $relating
  95. * @param string $include
  96. * @return void
  97. */
  98. private static function has_one($relationship, &$parents, $relating_key, $include)
  99. {
  100. foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
  101. {
  102. $parents[$child->$relating_key]->ignore[$include] = $child;
  103. }
  104. }
  105. /**
  106. * Eagerly load a 1:* relationship.
  107. *
  108. * @param object $relationship
  109. * @param array $parents
  110. * @param string $relating_key
  111. * @param string $relating
  112. * @param string $include
  113. * @return void
  114. */
  115. private static function has_many($relationship, &$parents, $relating_key, $include)
  116. {
  117. foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
  118. {
  119. $parents[$child->$relating_key]->ignore[$include][$child->id] = $child;
  120. }
  121. }
  122. /**
  123. * Eagerly load a 1:1 belonging relationship.
  124. *
  125. * @param object $relationship
  126. * @param array $parents
  127. * @param string $relating_key
  128. * @param string $include
  129. * @return void
  130. */
  131. private static function belongs_to($relationship, &$parents, $relating_key, $include)
  132. {
  133. $keys = array();
  134. foreach ($parents as &$parent)
  135. {
  136. $keys[] = $parent->$relating_key;
  137. }
  138. $children = $relationship->where_in('id', array_unique($keys))->get();
  139. foreach ($parents as &$parent)
  140. {
  141. if (array_key_exists($parent->$relating_key, $children))
  142. {
  143. $parent->ignore[$include] = $children[$parent->$relating_key];
  144. }
  145. }
  146. }
  147. /**
  148. * Eagerly load a many-to-many relationship.
  149. *
  150. * @param object $relationship
  151. * @param array $parents
  152. * @param string $relating_key
  153. * @param string $relating_table
  154. * @param string $include
  155. *
  156. * @return void
  157. */
  158. private static function has_and_belongs_to_many($relationship, &$parents, $relating_key, $relating_table, $include)
  159. {
  160. // The model "has and belongs to many" method sets the SELECT clause; however, we need
  161. // to clear it here since we will be adding the foreign key to the select.
  162. $relationship->query->select = null;
  163. $relationship->query->where_in($relating_table.'.'.$relating_key, array_keys($parents));
  164. // The foreign key is added to the select to allow us to easily match the models back to their parents.
  165. // Otherwise, there would be no apparent connection between the models to allow us to match them.
  166. $children = $relationship->query->get(array(Model::table(get_class($relationship)).'.*', $relating_table.'.'.$relating_key));
  167. $class = get_class($relationship);
  168. foreach ($children as $child)
  169. {
  170. $related = new $class;
  171. $related->attributes = (array) $child;
  172. $related->exists = true;
  173. // Remove the foreign key since it was only added to the query to help match the models.
  174. unset($related->attributes[$relating_key]);
  175. $parents[$child->$relating_key]->ignore[$include][$child->id] = $related;
  176. }
  177. }
  178. }