Skip to content

Commit

Permalink
Add refresh
Browse files Browse the repository at this point in the history
Closes #139
  • Loading branch information
baopham committed Apr 19, 2018
1 parent 4107c76 commit 4d30663
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Supports all key types - primary hash key and composite keys.
* [limit() and take()](#limit-and-take)
* [firstOrFail()](#firstorfail)
* [findOrFail()](#findorfail)
* [refresh()](#refresh)
* [Query scope](#query-scope)
* [REMOVE — Deleting Attributes From An Item](#remove--deleting-attributes-from-an-item)
* [toSql() Style](#tosql-style)
Expand Down Expand Up @@ -231,6 +232,13 @@ $model->findOrFail('foo');
$model->findOrFail(['id' => 'foo', 'id2' => 'bar']);
```
#### refresh()
```php
$model = Model::first();
$model->refresh();
```
#### Query Scope
```php
Expand Down
15 changes: 15 additions & 0 deletions src/DynamoDbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public static function all($columns = [])
return $instance->newQuery()->get($columns);
}

public function refresh()
{
if (! $this->exists) {
return $this;
}

$query = $this->newQuery();

$refreshed = $query->find($this->getKeys());

$this->setRawAttributes($refreshed->toArray());

return $this;
}

/**
* @return DynamoDbQueryBuilder
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,26 @@ public function testUsingBothKeyAndFilterConditionsForModelWithIndex()
);
}

public function testRefresh()
{
$this->seed();

$model = $this->testModel->first();

$originalHash = $model->id;
$originalRange = $model->id2;
$originalName = $model->name;

$model->name = 'Modified Name';

$refreshed = $model->refresh();

$this->assertEquals($originalName, $model->name);
$this->assertEquals($originalHash, $model->id);
$this->assertEquals($originalRange, $model->id2);
$this->assertEquals($refreshed, $model);
}

public function seed($attributes = [], $exclude = [])
{
$item = [
Expand Down
18 changes: 18 additions & 0 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,24 @@ public function testUsingBothKeyAndFilterConditionsForModelWithIndex()
$this->assertUsingKeyAndFilterConditions(new PrimaryKeyWithIndexModel());
}

public function testRefresh()
{
$this->seed();

$model = $this->testModel->first();

$originalId = $model->id;
$originalName = $model->name;

$model->name = 'Modified Name';

$refreshed = $model->refresh();

$this->assertEquals($originalName, $model->name);
$this->assertEquals($originalName, $model->name);
$this->assertEquals($refreshed, $model);
}

protected function assertRemoveAttributes($item)
{
$this->assertNull($item->name);
Expand Down

0 comments on commit 4d30663

Please sign in to comment.