hydrator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php namespace System\DB\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. $models[$model->id] = $model;
  44. }
  45. return $models;
  46. }
  47. /**
  48. * Eagerly load a relationship.
  49. *
  50. * @param object $eloquent
  51. * @param array $parents
  52. * @param string $include
  53. * @return void
  54. */
  55. private static function eagerly($eloquent, &$parents, $include)
  56. {
  57. // We temporarily spoof the belongs_to key to allow the query to be fetched without
  58. // any problems, since the belongs_to method actually gets the attribute.
  59. $eloquent->attributes[$spoof = $include.'_id'] = 0;
  60. $relationship = $eloquent->$include();
  61. unset($eloquent->attributes[$spoof]);
  62. // Reset the WHERE clause and bindings on the query. We'll add our own WHERE clause soon.
  63. $relationship->query->reset_where();
  64. // Initialize the relationship attribute on the parents. As expected, "many" relationships
  65. // are initialized to an array and "one" relationships are initialized to null.
  66. foreach ($parents as &$parent)
  67. {
  68. $parent->ignore[$include] = (in_array($eloquent->relating, array('has_many', 'has_and_belongs_to_many'))) ? array() : null;
  69. }
  70. if (in_array($relating = $eloquent->relating, array('has_one', 'has_many', 'belongs_to')))
  71. {
  72. return static::$relating($relationship, $parents, $eloquent->relating_key, $include);
  73. }
  74. else
  75. {
  76. static::has_and_belongs_to_many($relationship, $parents, $eloquent->relating_key, $eloquent->relating_table, $include);
  77. }
  78. }
  79. /**
  80. * Eagerly load a 1:1 relationship.
  81. *
  82. * @param object $relationship
  83. * @param array $parents
  84. * @param string $relating_key
  85. * @param string $relating
  86. * @param string $include
  87. * @return void
  88. */
  89. private static function has_one($relationship, &$parents, $relating_key, $include)
  90. {
  91. foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
  92. {
  93. $parents[$child->$relating_key]->ignore[$include] = $child;
  94. }
  95. }
  96. /**
  97. * Eagerly load a 1:* relationship.
  98. *
  99. * @param object $relationship
  100. * @param array $parents
  101. * @param string $relating_key
  102. * @param string $relating
  103. * @param string $include
  104. * @return void
  105. */
  106. private static function has_many($relationship, &$parents, $relating_key, $include)
  107. {
  108. foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
  109. {
  110. $parents[$child->$relating_key]->ignore[$include][$child->id] = $child;
  111. }
  112. }
  113. /**
  114. * Eagerly load a 1:1 belonging relationship.
  115. *
  116. * @param object $relationship
  117. * @param array $parents
  118. * @param string $relating_key
  119. * @param string $include
  120. * @return void
  121. */
  122. private static function belongs_to($relationship, &$parents, $relating_key, $include)
  123. {
  124. $keys = array();
  125. foreach ($parents as &$parent)
  126. {
  127. $keys[] = $parent->$relating_key;
  128. }
  129. $children = $relationship->where_in('id', array_unique($keys))->get();
  130. foreach ($parents as &$parent)
  131. {
  132. if (array_key_exists($parent->$relating_key, $children))
  133. {
  134. $parent->ignore[$include] = $children[$parent->$relating_key];
  135. }
  136. }
  137. }
  138. /**
  139. * Eagerly load a many-to-many relationship.
  140. *
  141. * @param object $relationship
  142. * @param array $parents
  143. * @param string $relating_key
  144. * @param string $relating_table
  145. * @param string $include
  146. *
  147. * @return void
  148. */
  149. private static function has_and_belongs_to_many($relationship, &$parents, $relating_key, $relating_table, $include)
  150. {
  151. // The model "has and belongs to many" method sets the SELECT clause; however, we need
  152. // to clear it here since we will be adding the foreign key to the select.
  153. $relationship->query->select = null;
  154. $relationship->query->where_in($relating_table.'.'.$relating_key, array_keys($parents));
  155. // The foreign key is added to the select to allow us to easily match the models back to their parents.
  156. // Otherwise, there would be no apparent connection between the models to allow us to match them.
  157. $children = $relationship->query->get(array(Model::table(get_class($relationship)).'.*', $relating_table.'.'.$relating_key));
  158. $class = get_class($relationship);
  159. foreach ($children as $child)
  160. {
  161. $related = new $class;
  162. $related->attributes = (array) $child;
  163. $related->exists = true;
  164. // Remove the foreign key since it was added to the query to help match to the children.
  165. unset($related->attributes[$relating_key]);
  166. $parents[$child->$relating_key]->ignore[$include][$child->id] = $related;
  167. }
  168. }
  169. }