belongs_to.php 2.61 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
		$this->table->where_in($this->model->key(), array_unique($keys));
	}

	/**
	 * Match eagerly loaded child models to their parent models.
	 *
82 83
	 * @param  array  $children
	 * @param  array  $parents
Taylor Otwell committed
84 85 86 87 88 89
	 * @return void
	 */
	public function match($relationship, &$children, $parents)
	{
		$foreign = $this->foreign_key();

90 91
		$dictionary = array();

92
		foreach ($parents as $parent)
Taylor Otwell committed
93
		{
94
			$dictionary[$parent->get_key()] = $parent;
95
		}
96

97 98
		foreach ($children as $child)
		{
99
			if (array_key_exists($child->$foreign, $dictionary))
100
			{
101
				$child->relationships[$relationship] = $dictionary[$child->$foreign];
Taylor Otwell committed
102 103 104 105
			}
		}
	}

106 107 108 109 110 111 112
	/**
	 * Get the value of the foreign key from the base model.
	 *
	 * @return mixed
	 */
	public function foreign_value()
	{
113
		return $this->base->{$this->foreign};
114
	}
115 116 117 118 119 120 121 122 123 124 125 126 127
	
	/**
	* Bind an object over a belongs-to relation using its id.
	*
	* @return Eloquent
	*/
	
	public function bind($id)
	{
		$this->base->fill(array($this->foreign => $id))->save();

		return $this->base;
	}
128

129
}