From 4d30663a79420730357254bc499306b13cc0aaea Mon Sep 17 00:00:00 2001 From: Bao Pham Date: Fri, 20 Apr 2018 00:59:43 +0800 Subject: [PATCH] Add refresh Closes #139 --- README.md | 8 ++++++++ src/DynamoDbModel.php | 15 +++++++++++++++ tests/DynamoDbCompositeModelTest.php | 20 ++++++++++++++++++++ tests/DynamoDbModelTest.php | 18 ++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/README.md b/README.md index 83f21d5..a32a6d2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -231,6 +232,13 @@ $model->findOrFail('foo'); $model->findOrFail(['id' => 'foo', 'id2' => 'bar']); ``` +#### refresh() + +```php +$model = Model::first(); +$model->refresh(); +``` + #### Query Scope ```php diff --git a/src/DynamoDbModel.php b/src/DynamoDbModel.php index 51043fd..3cc5afd 100644 --- a/src/DynamoDbModel.php +++ b/src/DynamoDbModel.php @@ -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 */ diff --git a/tests/DynamoDbCompositeModelTest.php b/tests/DynamoDbCompositeModelTest.php index d2a28e3..7189c82 100644 --- a/tests/DynamoDbCompositeModelTest.php +++ b/tests/DynamoDbCompositeModelTest.php @@ -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 = [ diff --git a/tests/DynamoDbModelTest.php b/tests/DynamoDbModelTest.php index 38addef..8d71a24 100644 --- a/tests/DynamoDbModelTest.php +++ b/tests/DynamoDbModelTest.php @@ -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);