Commit 74c9780d authored by Jens Segers's avatar Jens Segers

Trying to improve readability for embedsMany

parent a1d94104
...@@ -69,6 +69,7 @@ class EmbedsMany extends Relation { ...@@ -69,6 +69,7 @@ class EmbedsMany extends Relation {
*/ */
public function addEagerConstraints(array $models) public function addEagerConstraints(array $models)
{ {
// There are no eager loading constraints.
} }
/** /**
...@@ -100,14 +101,9 @@ class EmbedsMany extends Relation { ...@@ -100,14 +101,9 @@ class EmbedsMany extends Relation {
{ {
foreach ($models as $model) foreach ($models as $model)
{ {
// Get raw attributes to skip relations and accessors. $results = $this->getEmbeddedRecords($model);
$attributes = $model->getAttributes();
$results = isset($attributes[$this->localKey]) ? $attributes[$this->localKey] : array(); $model->setRelation($relation, $this->toCollection($results));
$collection = $this->toCollection($results);
$model->setRelation($relation, $collection);
} }
return $models; return $models;
...@@ -120,10 +116,7 @@ class EmbedsMany extends Relation { ...@@ -120,10 +116,7 @@ class EmbedsMany extends Relation {
*/ */
public function getResults() public function getResults()
{ {
// Get embedded documents. return $this->toCollection($this->getEmbeddedRecords());
$results = $this->getEmbeddedRecords();
return $this->toCollection($results);
} }
/** /**
...@@ -171,21 +164,12 @@ class EmbedsMany extends Relation { ...@@ -171,21 +164,12 @@ class EmbedsMany extends Relation {
$model->setAttribute('_id', new MongoId); $model->setAttribute('_id', new MongoId);
} }
// Set timestamps. // Update timestamps.
if ($model->usesTimestamps()) $this->updateTimestamps($model);
{
$time = $model->freshTimestamp();
$model->setUpdatedAt($time);
$model->setCreatedAt($time);
}
$model->exists = true;
// Push the document to the database. // Push the document to the database.
$result = $this->query->push($this->localKey, $model->getAttributes(), true); $result = $this->query->push($this->localKey, $model->getAttributes(), true);
// Get existing embedded documents.
$documents = $this->getEmbeddedRecords(); $documents = $this->getEmbeddedRecords();
// Add the document to the parent model. // Add the document to the parent model.
...@@ -193,6 +177,9 @@ class EmbedsMany extends Relation { ...@@ -193,6 +177,9 @@ class EmbedsMany extends Relation {
$this->setEmbeddedRecords($documents); $this->setEmbeddedRecords($documents);
// Mark the model as existing.
$model->exists = true;
return $result ? $model : false; return $result ? $model : false;
} }
...@@ -205,15 +192,10 @@ class EmbedsMany extends Relation { ...@@ -205,15 +192,10 @@ class EmbedsMany extends Relation {
protected function performUpdate(Model $model) protected function performUpdate(Model $model)
{ {
// Update timestamps. // Update timestamps.
if ($model->usesTimestamps()) $this->updateTimestamps($model);
{
$time = $model->freshTimestamp();
$model->setUpdatedAt($time);
}
// Convert the id to MongoId if necessary. // Get the correct foreign key value.
$id = $this->query->getQuery()->convertKey($model->getKey()); $id = $this->getForeignKeyValue($model->getKey());
// Update document in database. // Update document in database.
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id) $result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
...@@ -315,11 +297,7 @@ class EmbedsMany extends Relation { ...@@ -315,11 +297,7 @@ class EmbedsMany extends Relation {
// Pull the documents from the database. // Pull the documents from the database.
foreach ($ids as $id) foreach ($ids as $id)
{ {
// Convert the id to MongoId if necessary. $this->query->pull($this->localKey, array($primaryKey => $this->getForeignKeyValue($id)));
$id = $this->query->getQuery()->convertKey($id);
$this->query->pull($this->localKey, array($primaryKey => $id));
$count++; $count++;
} }
...@@ -373,9 +351,12 @@ class EmbedsMany extends Relation { ...@@ -373,9 +351,12 @@ class EmbedsMany extends Relation {
$models = array(); $models = array();
// Wrap documents in model objects. // Wrap documents in model objects.
foreach ($results as $result) foreach ($results as $model)
{ {
$model = $this->related->newFromBuilder($result); if ( ! $model instanceof Model)
{
$model = $this->related->newFromBuilder($model);
}
// Attatch the parent relation to the embedded model. // Attatch the parent relation to the embedded model.
$model->setRelation($this->foreignKey, $this->parent); $model->setRelation($this->foreignKey, $this->parent);
...@@ -391,10 +372,15 @@ class EmbedsMany extends Relation { ...@@ -391,10 +372,15 @@ class EmbedsMany extends Relation {
* *
* @return array * @return array
*/ */
protected function getEmbeddedRecords() protected function getEmbeddedRecords(Model $model = null)
{ {
if (is_null($model))
{
$model = $this->parent;
}
// Get raw attributes to skip relations and accessors. // Get raw attributes to skip relations and accessors.
$attributes = $this->parent->getAttributes(); $attributes = $model->getAttributes();
return isset($attributes[$this->localKey]) ? $attributes[$this->localKey] : array(); return isset($attributes[$this->localKey]) ? $attributes[$this->localKey] : array();
} }
...@@ -417,4 +403,36 @@ class EmbedsMany extends Relation { ...@@ -417,4 +403,36 @@ class EmbedsMany extends Relation {
$this->parent->setRelation($this->relation, $this->getResults()); $this->parent->setRelation($this->relation, $this->getResults());
} }
/**
* Update the creation and update timestamps.
*
* @return void
*/
protected function updateTimestamps(Model $model)
{
$time = $model->freshTimestamp();
if ( ! $model->isDirty(Model::UPDATED_AT))
{
$model->setUpdatedAt($time);
}
if ( ! $model->exists && ! $model->isDirty(Model::CREATED_AT))
{
$model->setCreatedAt($time);
}
}
/**
* Get the foreign key value for the relation.
*
* @param mixed $id
* @return mixed
*/
protected function getForeignKeyValue($id)
{
// Convert the id to MongoId if necessary.
return $this->getBaseQuery()->convertKey($id);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment