From 2269286be2533e7a44bc16450548edb017c5eb67 Mon Sep 17 00:00:00 2001 From: Petros Date: Wed, 26 Sep 2018 00:01:27 +0300 Subject: [PATCH 1/2] Propagate deletations and restoriations when using ModelTrait --- src/ModelObserver.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/ModelObserver.php b/src/ModelObserver.php index 3bc6b0d..f115703 100644 --- a/src/ModelObserver.php +++ b/src/ModelObserver.php @@ -32,7 +32,7 @@ public function __construct(DynamoDbClientInterface $dynamoDb) $this->attributeFilter = $dynamoDb->getAttributeFilter(); } - public function saved($model) + protected function _save($model) { $attrs = $model->attributesToArray(); // $this->attributeFilter->filter($attrs); @@ -45,4 +45,38 @@ public function saved($model) Log::info($e); } } + + protected function _delete($model) + { + $key = [$model->getKeyName() => $model->getKey()]; + + try { + $this->dynamoDbClient->deleteItem([ + 'TableName' => $model->getDynamoDbTableName(), + 'Key' => $this->marshaler->marshalItem($key), + ]); + } catch (Exception $e) { + Log::info($e); + } + } + + public function created($model) + { + $this->_save($model); + } + + public function updated($model) + { + $this->_save($model); + } + + public function deleted($model) + { + $this->_delete($model); + } + + public function restored($model) + { + $this->_save($model); + } } From 4c5b11b2bc8a3034b25cb38854430a0dc985f525 Mon Sep 17 00:00:00 2001 From: Petros Date: Wed, 26 Sep 2018 21:31:49 +0300 Subject: [PATCH 2/2] Refactor ModelObserver --- src/ModelObserver.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ModelObserver.php b/src/ModelObserver.php index f115703..241936c 100644 --- a/src/ModelObserver.php +++ b/src/ModelObserver.php @@ -32,21 +32,21 @@ public function __construct(DynamoDbClientInterface $dynamoDb) $this->attributeFilter = $dynamoDb->getAttributeFilter(); } - protected function _save($model) + private function saveToDynamoDb($model) { $attrs = $model->attributesToArray(); - // $this->attributeFilter->filter($attrs); + try { $this->dynamoDbClient->putItem([ 'TableName' => $model->getDynamoDbTableName(), 'Item' => $this->marshaler->marshalItem($attrs), ]); } catch (Exception $e) { - Log::info($e); + Log::error($e); } } - protected function _delete($model) + private function deleteFromDynamoDb($model) { $key = [$model->getKeyName() => $model->getKey()]; @@ -56,27 +56,27 @@ protected function _delete($model) 'Key' => $this->marshaler->marshalItem($key), ]); } catch (Exception $e) { - Log::info($e); + Log::error($e); } } public function created($model) { - $this->_save($model); + $this->saveToDynamoDb($model); } public function updated($model) { - $this->_save($model); + $this->saveToDynamoDb($model); } public function deleted($model) { - $this->_delete($model); + $this->deleteFromDynamoDb($model); } public function restored($model) { - $this->_save($model); + $this->saveToDynamoDb($model); } }