123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php namespace System\DB\Eloquent;
- class Hydrator {
-
- public static function hydrate($eloquent)
- {
- $results = static::base(get_class($eloquent), $eloquent->query->get());
- if (count($results) > 0)
- {
- foreach ($eloquent->includes as $include)
- {
- if ( ! method_exists($eloquent, $include))
- {
- throw new \Exception("Attempting to eager load [$include], but the relationship is not defined.");
- }
- static::eagerly($eloquent, $results, $include);
- }
- }
- return $results;
- }
-
- private static function base($class, $results)
- {
- $models = array();
- foreach ($results as $result)
- {
- $model = new $class;
- $model->attributes = (array) $result;
- $model->exists = true;
- $models[$model->id] = $model;
- }
- return $models;
- }
-
- private static function eagerly($eloquent, &$parents, $include)
- {
-
-
- $eloquent->attributes[$spoof = $include.'_id'] = 0;
- $relationship = $eloquent->$include();
- unset($eloquent->attributes[$spoof]);
-
-
- $relationship->query->reset_where();
-
-
- foreach ($parents as &$parent)
- {
- $parent->ignore[$include] = (in_array($eloquent->relating, array('has_many', 'has_and_belongs_to_many'))) ? array() : null;
- }
- if (in_array($relating = $eloquent->relating, array('has_one', 'has_many', 'belongs_to')))
- {
- return static::$relating($relationship, $parents, $eloquent->relating_key, $include);
- }
- else
- {
- static::has_and_belongs_to_many($relationship, $parents, $eloquent->relating_key, $eloquent->relating_table, $include);
- }
- }
-
- private static function has_one($relationship, &$parents, $relating_key, $include)
- {
- foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
- {
- $parents[$child->$relating_key]->ignore[$include] = $child;
- }
- }
-
- private static function has_many($relationship, &$parents, $relating_key, $include)
- {
- foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
- {
- $parents[$child->$relating_key]->ignore[$include][$child->id] = $child;
- }
- }
-
- private static function belongs_to($relationship, &$parents, $relating_key, $include)
- {
- $keys = array();
- foreach ($parents as &$parent)
- {
- $keys[] = $parent->$relating_key;
- }
- $children = $relationship->where_in('id', array_unique($keys))->get();
- foreach ($parents as &$parent)
- {
- if (array_key_exists($parent->$relating_key, $children))
- {
- $parent->ignore[$include] = $children[$parent->$relating_key];
- }
- }
- }
-
- private static function has_and_belongs_to_many($relationship, &$parents, $relating_key, $relating_table, $include)
- {
-
-
- $relationship->query->select = null;
- $relationship->query->where_in($relating_table.'.'.$relating_key, array_keys($parents));
-
-
- $children = $relationship->query->get(array(Model::table(get_class($relationship)).'.*', $relating_table.'.'.$relating_key));
- $class = get_class($relationship);
- foreach ($children as $child)
- {
- $related = new $class;
- $related->attributes = (array) $child;
- $related->exists = true;
-
- unset($related->attributes[$relating_key]);
- $parents[$child->$relating_key]->ignore[$include][$child->id] = $related;
- }
- }
- }
|