123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php namespace Laravel\Database\Eloquent\Relationships;
- class Belongs_To extends Relationship {
-
- public function results()
- {
- return parent::first();
- }
-
- public function update($attributes)
- {
- $attributes = ($attributes instanceof Model) ? $attributes->get_dirty() : $attributes;
- return $this->model->update($this->foreign_value(), $attributes);
- }
-
- protected function constrain()
- {
- $this->table->where($this->base->key(), '=', $this->foreign_value());
- }
-
- public function initialize(&$parents, $relationship)
- {
- foreach ($parents as &$parent)
- {
- $parent->relationships[$relationship] = null;
- }
- }
-
- public function eagerly_constrain($results)
- {
- $keys = array();
-
-
-
- foreach ($results as $result)
- {
- if ( ! is_null($key = $result->{$this->foreign_key()}))
- {
- $keys[] = $key;
- }
- }
- $this->table->where_in($this->model->key(), array_unique($keys));
- }
-
- public function match($relationship, &$children, $parents)
- {
- $foreign = $this->foreign_key();
- foreach ($children as &$child)
- {
- $parent = array_first($parents, function($k, $v) use ($child, $foreign)
- {
- return $v->get_key() == $child->$foreign;
- });
- if ( ! is_null($parent))
- {
- $child->relationships[$relationship] = $parent;
- }
- }
- }
-
- public function foreign_value()
- {
- return $this->base->get_attribute($this->foreign);
- }
- }
|