Skip to content

Commit 413af4e

Browse files
committed
Fix removeAttribute not syncing back the new attribute values
Closes #152
1 parent 7367d68 commit 413af4e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/DynamoDbQueryBuilder.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,23 @@ public function removeAttribute(...$attributes)
586586

587587
$this->resetExpressions();
588588

589+
/** @var \Aws\Result $result */
589590
$result = DynamoDb::table($this->model->getTable())
590591
->setKey($key)
591592
->setUpdateExpression($this->updateExpression->remove($attributes))
592593
->setExpressionAttributeNames($this->expressionAttributeNames->all())
594+
->setReturnValues('ALL_NEW')
593595
->prepare($this->client)
594596
->updateItem();
595597

596-
return array_get($result, '@metadata.statusCode') === 200;
598+
$success = array_get($result, '@metadata.statusCode') === 200;
599+
600+
if ($success) {
601+
$this->model->setRawAttributes(DynamoDb::unmarshalItem($result->get('Attributes')));
602+
$this->model->syncOriginal();
603+
}
604+
605+
return $success;
597606
}
598607

599608
public function delete()

tests/DynamoDbCompositeModelTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ public function testRemoveNestedAttribute()
495495
->where('id2', 'bar')
496496
->removeAttribute('nested.foo');
497497

498+
$this->assertArrayNotHasKey('foo', $this->testModel->nested);
499+
$this->assertFalse($this->testModel->isDirty());
500+
498501
$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);
499502
$this->assertArrayNotHasKey('foo', $item->nested);
500503
}
@@ -511,8 +514,10 @@ public function testRemoveAttributesOnQuery()
511514
->where('id2', 'bar')
512515
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
513516

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

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

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

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

530534
$this->assertRemoveAttributes($item);
535+
$this->assertFalse($item->isDirty());
536+
537+
$item = $this->testModel->first();
538+
$this->assertRemoveAttributes($item);
531539
}
532540

533541
public function testAfterForQueryOperation()

tests/DynamoDbNonCompositeModelTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,9 @@ public function testRemoveNestedAttribute()
786786
->where('id', 'foo')
787787
->removeAttribute('nested.foo');
788788

789+
$this->assertArrayNotHasKey('foo', $this->testModel->nested);
790+
$this->assertFalse($this->testModel->isDirty());
791+
789792
$item = $this->testModel->find('foo');
790793
$this->assertArrayNotHasKey('foo', $item->nested);
791794
}
@@ -798,6 +801,9 @@ public function testRemoveAttributesOnQuery()
798801
->where('id', 'foo')
799802
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
800803

804+
$this->assertRemoveAttributes($this->testModel);
805+
$this->assertFalse($this->testModel->isDirty());
806+
801807
$item = $this->testModel->find('foo');
802808
$this->assertRemoveAttributes($item);
803809
}
@@ -808,9 +814,12 @@ public function testRemoveAttributesOnModel()
808814

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

813818
$this->assertRemoveAttributes($item);
819+
$this->assertFalse($item->isDirty());
820+
821+
$item = $this->testModel->first();
822+
$this->assertRemoveAttributes($item);
814823
}
815824

816825
public function testAfterForQueryOperation()

0 commit comments

Comments
 (0)