belongs_to.php 2.39 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php namespace Laravel\Database\Eloquent\Relationships;

class Belongs_To extends Relationship {

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

	/**
16 17
	 * Update the parent model of the relationship.
	 *
18
	 * @param  Model|array  $attributes
19 20 21 22
	 * @return int
	 */
	public function update($attributes)
	{
23 24
		$attributes = ($attributes instanceof Model) ? $attributes->get_dirty() : $attributes;

25 26 27 28
		return $this->model->update($this->foreign_value(), $attributes);
	}

	/**
Taylor Otwell committed
29 30 31 32 33 34
	 * Set the proper constraints on the relationship table.
	 *
	 * @return void
	 */
	protected function constrain()
	{
35
		$this->table->where($this->model->key(), '=', $this->foreign_value());
Taylor Otwell committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	}

	/**
	 * 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;
		}
	}

	/**
	 * Set the proper constraints on the relationship table for an eager load.
	 *
	 * @param  array  $results
	 * @return void
	 */
	public function eagerly_constrain($results)
	{
		$keys = array();

		// Inverse one-to-many relationships require us to gather the keys from the
		// parent models and use those keys when setting the constraint since we
		// are looking for the parent of a child model in this relationship.
		foreach ($results as $result)
		{
68 69 70 71
			if ( ! is_null($key = $result->{$this->foreign_key()}))
			{
				$keys[] = $key;
			}
Taylor Otwell committed
72 73
		}

74 75
		if (count($keys) == 0) $keys = array(0);

Taylor Otwell committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		$this->table->where_in($this->model->key(), array_unique($keys));
	}

	/**
	 * Match eagerly loaded child models to their parent models.
	 *
	 * @param  array  $children
	 * @param  array  $parents
	 * @return void
	 */
	public function match($relationship, &$children, $parents)
	{
		$foreign = $this->foreign_key();

		foreach ($children as &$child)
		{
92
			$parent = array_first($parents, function($k, $v) use ($child, $foreign)
Taylor Otwell committed
93
			{
94 95 96 97 98 99
				return $v->get_key() == $child->$foreign;
			});

			if ( ! is_null($parent))
			{
				$child->relationships[$relationship] = $parent;
Taylor Otwell committed
100 101 102 103
			}
		}
	}

104 105 106 107 108 109 110 111 112 113
	/**
	 * Get the value of the foreign key from the base model.
	 *
	 * @return mixed
	 */
	public function foreign_value()
	{
		return $this->base->get_attribute($this->foreign);
	}

Taylor Otwell committed
114
}