has_one.php 1.07 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php namespace Laravel\Database\Eloquent\Relationships;

class Has_One extends Has_One_Or_Many {

	/**
	 * Get the properly hydrated results for the relationship.
	 *
	 * @return Model
	 */
	public function results()
	{
		return parent::first();
	}

	/**
	 * Initialize a relationship on an array of parent models.
	 *
	 * @param  array   $parents
	 * @param  string  $relationship
	 * @return void
	 */
	public function initialize(&$parents, $relationship)
	{
		foreach ($parents as &$parent)
		{
			$parent->relationships[$relationship] = null;
		}
	}

	/**
	 * Match eagerly loaded child models to their parent models.
	 *
33 34
	 * @param  array  $parents
	 * @param  array  $children
Taylor Otwell committed
35 36 37 38 39 40
	 * @return void
	 */
	public function match($relationship, &$parents, $children)
	{
		$foreign = $this->foreign_key();

41 42
		$dictionary = array();

43
		foreach ($children as $child)
Taylor Otwell committed
44
		{
45
			$dictionary[$child->$foreign] = $child;
46
		}
47

48 49
		foreach ($parents as $parent)
		{
50
			if (array_key_exists($key = $parent->get_key(), $dictionary))
51
			{
52
				$parent->relationships[$relationship] = $dictionary[$key];
53
			}
Taylor Otwell committed
54 55 56 57
		}
	}

}