has_one_or_many.php 1.2 KB

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