has_one_or_many.php 1.21 KB
Newer Older
Taylor Otwell committed
1 2 3
<?php namespace Laravel\Database\Eloquent\Relationships;

use Laravel\Database\Eloquent\Model;
Taylor Otwell committed
4 5 6 7 8 9

class Has_One_Or_Many extends Relationship {

	/**
	 * Insert a new record for the association.
	 *
10
	 * @param  Model|array  $attributes
Taylor Otwell committed
11 12 13 14
	 * @return bool
	 */
	public function insert($attributes)
	{
15 16
		$attributes = ($attributes instanceof Model) ? $attributes->attributes : $attributes;

Taylor Otwell committed
17 18
		$attributes[$this->foreign_key()] = $this->base->get_key();

19
		return $this->model->create($attributes);
Taylor Otwell committed
20 21 22
	}

	/**
23 24 25 26 27 28 29 30 31
	 * Update a record for the association.
	 *
	 * @param  array  $attributes
	 * @return bool
	 */
	public function update(array $attributes)
	{
		if ($this->model->timestamps())
		{
32
			$attributes['updated_at'] = new \DateTime;
33 34 35 36 37 38
		}

		return $this->table->update($attributes);
	}

	/**
Taylor Otwell committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	 * Set the proper constraints on the relationship table.
	 *
	 * @return void
	 */
	protected function constrain()
	{
		$this->table->where($this->foreign_key(), '=', $this->base->get_key());
	}

	/**
	 * Set the proper constraints on the relationship table for an eager load.
	 *
	 * @param  array  $results
	 * @return void
	 */
	public function eagerly_constrain($results)
	{
56
		$this->table->where_in($this->foreign_key(), $this->keys($results));
Taylor Otwell committed
57 58 59
	}

}