has_many.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php namespace Laravel\Database\Eloquent\Relationships;
  2. class Has_Many extends Has_One_Or_Many {
  3. /**
  4. * Get the properly hydrated results for the relationship.
  5. *
  6. * @return array
  7. */
  8. public function results()
  9. {
  10. return parent::get();
  11. }
  12. /**
  13. * Sync the association table with an array of models.
  14. *
  15. * @param mixed $models
  16. * @return bool
  17. */
  18. public function save($models)
  19. {
  20. // If the given "models" are not an array, we'll force them into an array so
  21. // we can conveniently loop through them and insert all of them into the
  22. // related database table assigned to the associated model instance.
  23. if ( ! is_array($models)) $models = array($models);
  24. $current = $this->table->lists($this->model->key());
  25. foreach ($models as $attributes)
  26. {
  27. $class = get_class($this->model);
  28. // If the "attributes" are actually an array of the related model we'll
  29. // just use the existing instance instead of creating a fresh model
  30. // instance for the attributes. This allows for validation.
  31. if ($attributes instanceof $class)
  32. {
  33. $model = $attributes;
  34. }
  35. else
  36. {
  37. $model = $this->fresh_model($attributes);
  38. }
  39. // We'll need to associate the model with its parent, so we'll set the
  40. // foreign key on the model to the key of the parent model, making
  41. // sure that the two models are associated in the database.
  42. $foreign = $this->foreign_key();
  43. $model->$foreign = $this->base->get_key();
  44. $id = $model->get_key();
  45. $model->exists = ( ! is_null($id) and in_array($id, $current));
  46. // Before saving we'll force the entire model to be "dirty" so all of
  47. // the attributes are saved. It shouldn't affect the updates as
  48. // saving all the attributes shouldn't hurt anything.
  49. $model->original = array();
  50. $model->save();
  51. }
  52. return true;
  53. }
  54. /**
  55. * Initialize a relationship on an array of parent models.
  56. *
  57. * @param array $parents
  58. * @param string $relationship
  59. * @return void
  60. */
  61. public function initialize(&$parents, $relationship)
  62. {
  63. foreach ($parents as &$parent)
  64. {
  65. $parent->relationships[$relationship] = array();
  66. }
  67. }
  68. /**
  69. * Match eagerly loaded child models to their parent models.
  70. *
  71. * @param array $parents
  72. * @param array $children
  73. * @return void
  74. */
  75. public function match($relationship, &$parents, $children)
  76. {
  77. $foreign = $this->foreign_key();
  78. $dictionary = array();
  79. foreach ($children as $child)
  80. {
  81. $dictionary[$child->$foreign][] = $child;
  82. }
  83. foreach ($parents as $parent)
  84. {
  85. if (array_key_exists($key = $parent->get_key(), $dictionary))
  86. {
  87. $parent->relationships[$relationship] = $dictionary[$key];
  88. }
  89. }
  90. }
  91. }