Skip to content

Commit 7367d68

Browse files
authored
Merge pull request #158 from pellaras/master
Propagate deletations and restoriations when using ModelTrait
2 parents 8b0c18e + 4c5b11b commit 7367d68

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/ModelObserver.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,51 @@ public function __construct(DynamoDbClientInterface $dynamoDb)
3232
$this->attributeFilter = $dynamoDb->getAttributeFilter();
3333
}
3434

35-
public function saved($model)
35+
private function saveToDynamoDb($model)
3636
{
3737
$attrs = $model->attributesToArray();
38-
// $this->attributeFilter->filter($attrs);
38+
3939
try {
4040
$this->dynamoDbClient->putItem([
4141
'TableName' => $model->getDynamoDbTableName(),
4242
'Item' => $this->marshaler->marshalItem($attrs),
4343
]);
4444
} catch (Exception $e) {
45-
Log::info($e);
45+
Log::error($e);
4646
}
4747
}
48+
49+
private function deleteFromDynamoDb($model)
50+
{
51+
$key = [$model->getKeyName() => $model->getKey()];
52+
53+
try {
54+
$this->dynamoDbClient->deleteItem([
55+
'TableName' => $model->getDynamoDbTableName(),
56+
'Key' => $this->marshaler->marshalItem($key),
57+
]);
58+
} catch (Exception $e) {
59+
Log::error($e);
60+
}
61+
}
62+
63+
public function created($model)
64+
{
65+
$this->saveToDynamoDb($model);
66+
}
67+
68+
public function updated($model)
69+
{
70+
$this->saveToDynamoDb($model);
71+
}
72+
73+
public function deleted($model)
74+
{
75+
$this->deleteFromDynamoDb($model);
76+
}
77+
78+
public function restored($model)
79+
{
80+
$this->saveToDynamoDb($model);
81+
}
4882
}

0 commit comments

Comments
 (0)