|
| 1 | +<?php namespace Jenssegers\Mongodb\Relations; |
| 2 | + |
| 3 | +use Illuminate\Database\Eloquent\Model; |
| 4 | +use Illuminate\Database\Eloquent\Collection; |
| 5 | + |
| 6 | +use MongoId; |
| 7 | + |
| 8 | +class EmbedsMany extends EmbeddedRelation { |
| 9 | + |
| 10 | + /** |
| 11 | + * The parent collection attribute where the related are stored. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $collection; |
| 16 | + |
| 17 | + /** |
| 18 | + * Create a new has many relationship instance. |
| 19 | + * |
| 20 | + * @param \Illuminate\Database\Eloquent\Model $parent |
| 21 | + * @param string $related |
| 22 | + * @param string $collection |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function __construct(Model $parent, $related, $collection) |
| 26 | + { |
| 27 | + $this->collection = $collection; |
| 28 | + |
| 29 | + parent::__construct($parent, $related); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Get the results of the relationship. |
| 34 | + * |
| 35 | + * @return Illuminate\Database\Eloquent\Collection |
| 36 | + */ |
| 37 | + public function getResults() |
| 38 | + { |
| 39 | + $models = new Collection(); |
| 40 | + |
| 41 | + $modelsAttributes = $this->parent->getAttribute($this->collection); |
| 42 | + |
| 43 | + if (is_array($modelsAttributes)) |
| 44 | + { |
| 45 | + foreach ($modelsAttributes as $attributes) |
| 46 | + { |
| 47 | + $models->push(new $this->related($attributes)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return $models; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new instance and attach it to the parent model. |
| 56 | + * |
| 57 | + * @param array $attributes |
| 58 | + * @return \Illuminate\Database\Eloquent\Model |
| 59 | + */ |
| 60 | + public function build(array $attributes) |
| 61 | + { |
| 62 | + if ( ! isset($attributes['_id'])) $attributes['_id'] = new MongoId; |
| 63 | + |
| 64 | + $collection = $this->parent->getAttribute($this->collection); |
| 65 | + $collection[''.$attributes['_id']] = $attributes; |
| 66 | + $this->parent->setAttribute($this->collection, $collection); |
| 67 | + |
| 68 | + return new $this->related($attributes); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Attach an instance to the parent model. |
| 73 | + * |
| 74 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 75 | + * @return \Illuminate\Database\Eloquent\Model |
| 76 | + */ |
| 77 | + public function add($model) |
| 78 | + { |
| 79 | + return $this->build($model->toArray()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create a new instance, attach it to the parent model and save this model. |
| 84 | + * |
| 85 | + * @param array $attributes |
| 86 | + * @return \Illuminate\Database\Eloquent\Model |
| 87 | + */ |
| 88 | + public function create(array $attributes) |
| 89 | + { |
| 90 | + $instance = $this->build($attributes); |
| 91 | + |
| 92 | + $this->parent->save(); |
| 93 | + |
| 94 | + return $instance; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Attach a model instance to the parent model and save this model. |
| 99 | + * |
| 100 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 101 | + * @return \Illuminate\Database\Eloquent\Model |
| 102 | + */ |
| 103 | + public function push($model) |
| 104 | + { |
| 105 | + return $this->create($model->toArray()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Create an array of new instances and attach them to the parent model. |
| 110 | + * |
| 111 | + * @param array|Traversable $modelsAttributes |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + public function buildMany($modelsAttributes) |
| 115 | + { |
| 116 | + $instances = array(); |
| 117 | + |
| 118 | + foreach ($modelsAttributes as $attributes) |
| 119 | + { |
| 120 | + $instances[] = $this->build($attributes); |
| 121 | + } |
| 122 | + |
| 123 | + return $instances; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Attach an array of new instances to the parent model. |
| 128 | + * |
| 129 | + * @param array|Traversable $models |
| 130 | + * @return array |
| 131 | + */ |
| 132 | + public function addMany($models) |
| 133 | + { |
| 134 | + $modelsAttributes = $this->getAttributesOf($models); |
| 135 | + |
| 136 | + return $this->buildMany($modelsAttributes); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Create an array of new instances, attach them to the parent model and save this model. |
| 141 | + * |
| 142 | + * @param array|Traversable $modelsAttributes |
| 143 | + * @return array |
| 144 | + */ |
| 145 | + public function createMany($modelsAttributes) |
| 146 | + { |
| 147 | + $instances = $this->buildMany($modelsAttributes); |
| 148 | + |
| 149 | + $this->parent->save(); |
| 150 | + |
| 151 | + return $instances; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Attach an array of new instances to the parent model and save this model. |
| 156 | + * |
| 157 | + * @param array|Traversable $models |
| 158 | + * @return array |
| 159 | + */ |
| 160 | + public function pushMany($models) |
| 161 | + { |
| 162 | + $modelsAttributes = $this->getAttributesOf($models); |
| 163 | + |
| 164 | + return $this->createMany($modelsAttributes); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Transform a list of models to a list of models' attributes |
| 169 | + * |
| 170 | + * @param array|Traversable $models |
| 171 | + * @return array |
| 172 | + */ |
| 173 | + protected function getAttributesOf($models) |
| 174 | + { |
| 175 | + $modelsAttributes = array(); |
| 176 | + |
| 177 | + foreach ($models as $key => $model) |
| 178 | + { |
| 179 | + $modelsAttributes[$key] = $model->getAttributes(); |
| 180 | + } |
| 181 | + |
| 182 | + return $modelsAttributes; |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments