Skip to content

Commit 74c9780

Browse files
committed
Trying to improve readability for embedsMany
1 parent a1d9410 commit 74c9780

File tree

1 file changed

+57
-39
lines changed

1 file changed

+57
-39
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function addConstraints()
6969
*/
7070
public function addEagerConstraints(array $models)
7171
{
72+
// There are no eager loading constraints.
7273
}
7374

7475
/**
@@ -100,14 +101,9 @@ public function match(array $models, Collection $results, $relation)
100101
{
101102
foreach ($models as $model)
102103
{
103-
// Get raw attributes to skip relations and accessors.
104-
$attributes = $model->getAttributes();
104+
$results = $this->getEmbeddedRecords($model);
105105

106-
$results = isset($attributes[$this->localKey]) ? $attributes[$this->localKey] : array();
107-
108-
$collection = $this->toCollection($results);
109-
110-
$model->setRelation($relation, $collection);
106+
$model->setRelation($relation, $this->toCollection($results));
111107
}
112108

113109
return $models;
@@ -120,10 +116,7 @@ public function match(array $models, Collection $results, $relation)
120116
*/
121117
public function getResults()
122118
{
123-
// Get embedded documents.
124-
$results = $this->getEmbeddedRecords();
125-
126-
return $this->toCollection($results);
119+
return $this->toCollection($this->getEmbeddedRecords());
127120
}
128121

129122
/**
@@ -171,28 +164,22 @@ protected function performInsert(Model $model)
171164
$model->setAttribute('_id', new MongoId);
172165
}
173166

174-
// Set timestamps.
175-
if ($model->usesTimestamps())
176-
{
177-
$time = $model->freshTimestamp();
178-
179-
$model->setUpdatedAt($time);
180-
$model->setCreatedAt($time);
181-
}
182-
183-
$model->exists = true;
167+
// Update timestamps.
168+
$this->updateTimestamps($model);
184169

185170
// Push the document to the database.
186171
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
187172

188-
// Get existing embedded documents.
189173
$documents = $this->getEmbeddedRecords();
190174

191175
// Add the document to the parent model.
192176
$documents[] = $model->getAttributes();
193177

194178
$this->setEmbeddedRecords($documents);
195179

180+
// Mark the model as existing.
181+
$model->exists = true;
182+
196183
return $result ? $model : false;
197184
}
198185

@@ -205,15 +192,10 @@ protected function performInsert(Model $model)
205192
protected function performUpdate(Model $model)
206193
{
207194
// Update timestamps.
208-
if ($model->usesTimestamps())
209-
{
210-
$time = $model->freshTimestamp();
211-
212-
$model->setUpdatedAt($time);
213-
}
195+
$this->updateTimestamps($model);
214196

215-
// Convert the id to MongoId if necessary.
216-
$id = $this->query->getQuery()->convertKey($model->getKey());
197+
// Get the correct foreign key value.
198+
$id = $this->getForeignKeyValue($model->getKey());
217199

218200
// Update document in database.
219201
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
@@ -315,11 +297,7 @@ public function destroy($ids = array())
315297
// Pull the documents from the database.
316298
foreach ($ids as $id)
317299
{
318-
// Convert the id to MongoId if necessary.
319-
$id = $this->query->getQuery()->convertKey($id);
320-
321-
$this->query->pull($this->localKey, array($primaryKey => $id));
322-
300+
$this->query->pull($this->localKey, array($primaryKey => $this->getForeignKeyValue($id)));
323301
$count++;
324302
}
325303

@@ -373,9 +351,12 @@ protected function toCollection(array $results = array())
373351
$models = array();
374352

375353
// Wrap documents in model objects.
376-
foreach ($results as $result)
354+
foreach ($results as $model)
377355
{
378-
$model = $this->related->newFromBuilder($result);
356+
if ( ! $model instanceof Model)
357+
{
358+
$model = $this->related->newFromBuilder($model);
359+
}
379360

380361
// Attatch the parent relation to the embedded model.
381362
$model->setRelation($this->foreignKey, $this->parent);
@@ -391,10 +372,15 @@ protected function toCollection(array $results = array())
391372
*
392373
* @return array
393374
*/
394-
protected function getEmbeddedRecords()
375+
protected function getEmbeddedRecords(Model $model = null)
395376
{
377+
if (is_null($model))
378+
{
379+
$model = $this->parent;
380+
}
381+
396382
// Get raw attributes to skip relations and accessors.
397-
$attributes = $this->parent->getAttributes();
383+
$attributes = $model->getAttributes();
398384

399385
return isset($attributes[$this->localKey]) ? $attributes[$this->localKey] : array();
400386
}
@@ -417,4 +403,36 @@ protected function setEmbeddedRecords(array $models)
417403
$this->parent->setRelation($this->relation, $this->getResults());
418404
}
419405

406+
/**
407+
* Update the creation and update timestamps.
408+
*
409+
* @return void
410+
*/
411+
protected function updateTimestamps(Model $model)
412+
{
413+
$time = $model->freshTimestamp();
414+
415+
if ( ! $model->isDirty(Model::UPDATED_AT))
416+
{
417+
$model->setUpdatedAt($time);
418+
}
419+
420+
if ( ! $model->exists && ! $model->isDirty(Model::CREATED_AT))
421+
{
422+
$model->setCreatedAt($time);
423+
}
424+
}
425+
426+
/**
427+
* Get the foreign key value for the relation.
428+
*
429+
* @param mixed $id
430+
* @return mixed
431+
*/
432+
protected function getForeignKeyValue($id)
433+
{
434+
// Convert the id to MongoId if necessary.
435+
return $this->getBaseQuery()->convertKey($id);
436+
}
437+
420438
}

0 commit comments

Comments
 (0)