Skip to content

Commit a9bc77f

Browse files
zoul0813baopham
authored andcommitted
Add model events and set exists, wasRecentlyCreated properties
* Close #50 * Close #53
1 parent db650f3 commit a9bc77f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/DynamoDbModel.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,32 @@ protected function setupDynamoDb()
109109

110110
public function save(array $options = [])
111111
{
112-
if (!$this->getKey()) {
113-
$this->fireModelEvent('creating');
112+
$create = !$this->exists;
113+
114+
if ($this->fireModelEvent('saving') === false) {
115+
return false;
116+
}
117+
118+
if ($create && $this->fireModelEvent('creating') === false) {
119+
return false;
114120
}
121+
if (!$create && $this->fireModelEvent('updating') === false) {
122+
return false;
123+
}
124+
125+
$saved = $this->newQuery()->save();
126+
127+
if (!$saved) {
128+
return $saved;
129+
}
130+
131+
$this->exists = true;
132+
$this->wasRecentlyCreated = $create;
133+
$this->fireModelEvent($create ? 'created' : 'updated', false);
134+
135+
$this->finishSave($options);
115136

116-
return $this->newQuery()->save();
137+
return $saved;
117138
}
118139

119140
public function update(array $attributes = [], array $options = [])

0 commit comments

Comments
 (0)