hydrator.php 7.63 KB
Newer Older
1 2
<?php namespace System\DB\Eloquent;

3
class Hydrator {
4 5 6 7 8 9 10

	/**
	 * Load the array of hydrated models.
	 *
	 * @param  object  $eloquent
	 * @return array
	 */
11
	public static function hydrate($eloquent)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	{
		// -----------------------------------------------------
		// Load the base models.
		// -----------------------------------------------------
		$results = static::base(get_class($eloquent), $eloquent->query->get());

		// -----------------------------------------------------
		// Load all of the eager relationships.
		// -----------------------------------------------------
		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, $include, $results);
			}
		}

		return $results;
	}

	/**
	 * Hydrate the base models for a query.
	 *
	 * @param  string  $class
41
	 * @param  array   $results
42 43
	 * @return array
	 */
44
	private static function base($class, $results)
45
	{
46
		$models = array();
47

48
		foreach ($results as $result)
49
		{
50
			$model = new $class;
51

52 53 54 55 56
			// -----------------------------------------------------
			// Set the attributes and existence flag on the model.
			// -----------------------------------------------------
			$model->attributes = (array) $result;
			$model->exists = true;
57 58

			// -----------------------------------------------------
59
			// The results are keyed by the ID on the record.
60
			// -----------------------------------------------------
61
			$models[$model->id] = $model;
62 63
		}

64
		return $models;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	}

	/**
	 * Eagerly load a relationship.
	 *
	 * @param  object  $eloquent
	 * @param  string  $include
	 * @param  array   $results
	 * @return void
	 */
	private static function eagerly($eloquent, $include, &$results)
	{
		// -----------------------------------------------------
		// Get the relationship Eloquent model.
		//
		// We spoof the "belongs_to" key to allow the query
		// to be fetched without any problems.
		// -----------------------------------------------------
		$eloquent->attributes[$spoof = $include.'_id'] = 0;

		$model = $eloquent->$include();

		unset($eloquent->attributes[$spoof]);

		// -----------------------------------------------------
90
		// Reset the WHERE clause and bindings on the query.
91 92 93 94 95 96 97 98 99 100 101 102
		// -----------------------------------------------------
		$model->query->where = 'WHERE 1 = 1';
		$model->query->bindings = array();

		// -----------------------------------------------------
		// Initialize the relationship on the parent models.
		// -----------------------------------------------------
		foreach ($results as &$result)
		{
			$result->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
		}

103 104 105
		// -----------------------------------------------------
		// Eagerly load the relationship.
		// -----------------------------------------------------
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		if ($eloquent->relating == 'has_one' or $eloquent->relating == 'has_many')
		{
			static::eagerly_load_one_or_many($eloquent->relating_key, $eloquent->relating, $include, $model, $results);
		}
		elseif ($eloquent->relating == 'belongs_to')
		{
			static::eagerly_load_belonging($eloquent->relating_key, $include, $model, $results);
		}
		else
		{
			static::eagerly_load_many_to_many($eloquent->relating_key, $eloquent->relating_table, strtolower(get_class($eloquent)).'_id', $include, $model, $results);
		}
	}

	/**
	 * Eagerly load a 1:1 or 1:* relationship.
	 *
	 * @param  string  $relating_key
	 * @param  string  $relating
	 * @param  string  $include
	 * @param  object  $model
	 * @param  array   $results
	 * @return void
	 */
	private static function eagerly_load_one_or_many($relating_key, $relating, $include, $model, &$results)
	{
		// -----------------------------------------------------
		// Get the related models.
		// -----------------------------------------------------
		$inclusions = $model->where_in($relating_key, array_keys($results))->get();

		// -----------------------------------------------------
		// Match the child models with their parent.
		// -----------------------------------------------------
		foreach ($inclusions as $key => $inclusion)
		{
			if ($relating == 'has_one')
			{
				$results[$inclusion->$relating_key]->ignore[$include] = $inclusion;
			}
			else
			{
				$results[$inclusion->$relating_key]->ignore[$include][$inclusion->id] = $inclusion;
			}
		}
	}

	/**
	 * Eagerly load a 1:1 belonging relationship.
	 *
	 * @param  string  $relating_key
	 * @param  string  $include
	 * @param  object  $model
	 * @param  array   $results
	 * @return void
	 */
	private static function eagerly_load_belonging($relating_key, $include, $model, &$results)
	{
		// -----------------------------------------------------
		// Gather the keys from the parent models.
		// -----------------------------------------------------
		$keys = array();

		foreach ($results as &$result)
		{
			$keys[] = $result->$relating_key;
		}

		// -----------------------------------------------------
		// Get the related models.
		// -----------------------------------------------------
		$inclusions = $model->where_in('id', array_unique($keys))->get();

		// -----------------------------------------------------
		// Match the child models with their parent.
		// -----------------------------------------------------
		foreach ($results as &$result)
		{
			$result->ignore[$include] = $inclusions[$result->$relating_key];
		}
	}

	/**
	 * Eagerly load a many-to-many relationship.
	 *
	 * @param  string  $relating_key
	 * @param  string  $relating_table
	 * @param  string  $foreign_key
	 * @param  string  $include
	 * @param  object  $model
	 * @param  array   $results
	 * @return void	
	 */
	private static function eagerly_load_many_to_many($relating_key, $relating_table, $foreign_key, $include, $model, &$results)
	{
		// -----------------------------------------------------
		// Reset the SELECT clause.
		// -----------------------------------------------------
		$model->query->select = null;

		// -----------------------------------------------------
		// Retrieve the raw results as stdClasses.
		//
		// We also add the foreign key to the select which will allow us
		// to match the models back to their parents.
		// -----------------------------------------------------
212 213 214
		$inclusions = $model->query
								->where_in($relating_key, array_keys($results))
								->get(\System\DB\Eloquent::table(get_class($model)).'.*', $relating_table.'.'.$foreign_key);
215 216 217 218 219 220 221 222 223 224 225 226 227

		// -----------------------------------------------------
		// Get the class name of the related model.
		// -----------------------------------------------------
		$class = get_class($model);

		// -----------------------------------------------------
		// Create the related models.
		// -----------------------------------------------------
		foreach ($inclusions as $inclusion)
		{
			$related = new $class;

228 229 230
			// -----------------------------------------------------
			// Set the attributes and existence flag on the model.
			// -----------------------------------------------------
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
			$related->exists = true;
			$related->attributes = (array) $inclusion;

			// -----------------------------------------------------
			// Remove the foreign key from the attributes since it
			// was only added to the query to help us match the models.
			// -----------------------------------------------------
			unset($related->attributes[$foreign_key]);

			// -----------------------------------------------------
			// Add the related model to the parent model's array.
			// -----------------------------------------------------
			$results[$inclusion->$foreign_key]->ignore[$include][$inclusion->id] = $related;
		}
	}

}