Skip to content

Commit

Permalink
Fix removeAttribute not syncing back the new attribute values
Browse files Browse the repository at this point in the history
Closes #152
  • Loading branch information
baopham committed Nov 10, 2018
1 parent 7367d68 commit 413af4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,23 @@ public function removeAttribute(...$attributes)

$this->resetExpressions();

/** @var \Aws\Result $result */
$result = DynamoDb::table($this->model->getTable())
->setKey($key)
->setUpdateExpression($this->updateExpression->remove($attributes))
->setExpressionAttributeNames($this->expressionAttributeNames->all())
->setReturnValues('ALL_NEW')
->prepare($this->client)
->updateItem();

return array_get($result, '@metadata.statusCode') === 200;
$success = array_get($result, '@metadata.statusCode') === 200;

if ($success) {
$this->model->setRawAttributes(DynamoDb::unmarshalItem($result->get('Attributes')));
$this->model->syncOriginal();
}

return $success;
}

public function delete()
Expand Down
12 changes: 10 additions & 2 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ public function testRemoveNestedAttribute()
->where('id2', 'bar')
->removeAttribute('nested.foo');

$this->assertArrayNotHasKey('foo', $this->testModel->nested);
$this->assertFalse($this->testModel->isDirty());

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);
$this->assertArrayNotHasKey('foo', $item->nested);
}
Expand All @@ -511,8 +514,10 @@ public function testRemoveAttributesOnQuery()
->where('id2', 'bar')
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);
$this->assertRemoveAttributes($this->testModel);
$this->assertFalse($this->testModel->isDirty());

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);
$this->assertRemoveAttributes($item);
}

Expand All @@ -525,9 +530,12 @@ public function testRemoveAttributesOnModel()

$item = $this->testModel->first();
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertRemoveAttributes($item);
$this->assertFalse($item->isDirty());

$item = $this->testModel->first();
$this->assertRemoveAttributes($item);
}

public function testAfterForQueryOperation()
Expand Down
11 changes: 10 additions & 1 deletion tests/DynamoDbNonCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ public function testRemoveNestedAttribute()
->where('id', 'foo')
->removeAttribute('nested.foo');

$this->assertArrayNotHasKey('foo', $this->testModel->nested);
$this->assertFalse($this->testModel->isDirty());

$item = $this->testModel->find('foo');
$this->assertArrayNotHasKey('foo', $item->nested);
}
Expand All @@ -798,6 +801,9 @@ public function testRemoveAttributesOnQuery()
->where('id', 'foo')
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');

$this->assertRemoveAttributes($this->testModel);
$this->assertFalse($this->testModel->isDirty());

$item = $this->testModel->find('foo');
$this->assertRemoveAttributes($item);
}
Expand All @@ -808,9 +814,12 @@ public function testRemoveAttributesOnModel()

$item = $this->testModel->first();
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertRemoveAttributes($item);
$this->assertFalse($item->isDirty());

$item = $this->testModel->first();
$this->assertRemoveAttributes($item);
}

public function testAfterForQueryOperation()
Expand Down

0 comments on commit 413af4e

Please sign in to comment.