has_one.php 1.02 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
		foreach ($parents as &$parent)
Taylor Otwell committed
42
		{
43
			$matching = array_first($children, function($k, $v) use (&$parent, $foreign)
44 45 46 47 48
			{
				return $v->$foreign == $parent->get_key();
			});

			$parent->relationships[$relationship] = $matching;
Taylor Otwell committed
49 50 51 52
		}
	}

}