1: <?php namespace Laravel\Database\Eloquent\Relationships;
2:
3: use Laravel\Database\Eloquent\Model;
4:
5: class Has_One_Or_Many extends Relationship {
6:
7: /**
8: * Insert a new record for the association.
9: *
10: * If save is successful, the model will be returned, otherwise false.
11: *
12: * @param Model|array $attributes
13: * @return Model|false
14: */
15: public function insert($attributes)
16: {
17: if ($attributes instanceof Model)
18: {
19: $attributes->set_attribute($this->foreign_key(), $this->base->get_key());
20:
21: return $attributes->save() ? $attributes : false;
22: }
23: else
24: {
25: $attributes[$this->foreign_key()] = $this->base->get_key();
26:
27: return $this->model->create($attributes);
28: }
29: }
30:
31: /**
32: * Update a record for the association.
33: *
34: * @param array $attributes
35: * @return bool
36: */
37: public function update(array $attributes)
38: {
39: if ($this->model->timestamps())
40: {
41: $attributes['updated_at'] = new \DateTime;
42: }
43:
44: return $this->table->update($attributes);
45: }
46:
47: /**
48: * Set the proper constraints on the relationship table.
49: *
50: * @return void
51: */
52: protected function constrain()
53: {
54: $this->table->where($this->foreign_key(), '=', $this->base->get_key());
55: }
56:
57: /**
58: * Set the proper constraints on the relationship table for an eager load.
59: *
60: * @param array $results
61: * @return void
62: */
63: public function eagerly_constrain($results)
64: {
65: $this->table->where_in($this->foreign_key(), $this->keys($results));
66: }
67:
68: }