Skip to content

Commit

Permalink
#94 - updated model/query to use a "better" hydration (#96)
Browse files Browse the repository at this point in the history
Closes #94 - updated model/query to use a "better" hydration, should prevent various issues from occurring when using mutators and other custom logic on models.
  • Loading branch information
zoul0813 authored and baopham committed Sep 30, 2017
1 parent eb4ec09 commit f9c2e57
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
9 changes: 0 additions & 9 deletions src/DynamoDbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,6 @@ public function newQuery()
return $builder;
}

public function setUnfillableAttributes($attributes)
{
$keysToFill = array_diff(array_keys($attributes), $this->fillable);

foreach ($keysToFill as $key) {
$this->setAttribute($key, $attributes[$key]);
}
}

public function hasCompositeKey()
{
return !empty($this->compositeKey);
Expand Down
21 changes: 7 additions & 14 deletions src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,13 @@ public function find($id, array $columns = [])
return $this->findMany($id, $columns);
}

$model = $this->model;

$model->setId($id);
$this->model->setId($id);

$key = $this->getDynamoDbKey();

$query = [
'ConsistentRead' => true,
'TableName' => $model->getTable(),
'TableName' => $this->model->getTable(),
'Key' => $key,
];

Expand All @@ -351,15 +349,11 @@ public function find($id, array $columns = [])
return;
}

$item = $model->unmarshalItem($item);

$model->fill($item);

$model->setUnfillableAttributes($item);
$item = $this->model->unmarshalItem($item);

$model->syncOriginal();
$model = $this->model->newInstance([], true);

$model->exists = true;
$model->setRawAttributes($item, true);

return $model;
}
Expand Down Expand Up @@ -547,9 +541,8 @@ protected function getAll($columns = [], $limit = -1, $use_iterator = true)

foreach ($iterator as $item) {
$item = $this->model->unmarshalItem($item);
$model = $this->model->newInstance($item, true);
$model->setUnfillableAttributes($item);
$model->syncOriginal();
$model = $this->model->newInstance([], true);
$model->setRawAttributes($item, true);
$results[] = $model;
}

Expand Down

0 comments on commit f9c2e57

Please sign in to comment.