Skip to content

Commit

Permalink
Merge pull request #72 from atk4/feature/preserve-dirty-flags-across-…
Browse files Browse the repository at this point in the history
…relad

Preserves "Dirty" flags for afterSave hooks.
  • Loading branch information
romaninsh committed Jul 25, 2016
2 parents bfc16ee + f424bfe commit 63c75ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ public function tryLoadBy($field, $value)
*
* @return $this
*/
public $_dirty_after_reload = [];

public function save($data = [])
{
if (!$this->persistence) {
Expand All @@ -900,6 +902,8 @@ public function save($data = [])
return $this;
}

$this->_dirty_after_save = [];

$is_update = $this->loaded();
if ($is_update) {
$data = [];
Expand Down Expand Up @@ -963,15 +967,19 @@ public function save($data = [])
$this->hook('afterInsert', [$this->id]);

if ($this->reload_after_save !== false) {
$d = $this->dirty;
$this->dirty = [];
$this->reload();
$this->_dirty_after_reload = $this->dirty;
$this->dirty = $d;
}
}

$this->hook('afterSave');


if ($this->loaded()) {
$this->dirty = [];
$this->dirty = $this->_dirty_after_reload;
}

return $this;
Expand Down
3 changes: 3 additions & 0 deletions src/Persistence_SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,10 @@ public function update(Model $m, $id, $data)

// if any rows were updated in database, and we had expressions, reload
if ($m->reload_after_save === true && $st->rowCount()) {
$d = $m->dirty;
$m->reload();
$m->_dirty_after_reload = $m->dirty;
$m->dirty = $d;
}
}

Expand Down

0 comments on commit 63c75ad

Please sign in to comment.