Commit 1c93c503 by Qiang Xue

Merge pull request #160 from creocoder/active-record-refactoring

ActiveRecord::insert() and ActiveRecord::update() refactoring
parents dd596914 e3f6faf5
...@@ -667,12 +667,11 @@ class ActiveRecord extends Model ...@@ -667,12 +667,11 @@ class ActiveRecord extends Model
*/ */
public function insert($runValidation = true, $attributes = null) public function insert($runValidation = true, $attributes = null)
{ {
if ($runValidation && !$this->validate($attributes)) { if ($runValidation && !$this->validate($attributes) || !$this->beforeSave(true)) {
return false; return false;
} }
if ($this->beforeSave(true)) {
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if ($values === array()) { if (empty($values)) {
foreach ($this->primaryKey() as $key) { foreach ($this->primaryKey() as $key) {
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null; $values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
} }
...@@ -696,8 +695,6 @@ class ActiveRecord extends Model ...@@ -696,8 +695,6 @@ class ActiveRecord extends Model
return true; return true;
} }
} }
return false;
}
/** /**
* Saves the changes to this active record into the associated database table. * Saves the changes to this active record into the associated database table.
...@@ -750,12 +747,11 @@ class ActiveRecord extends Model ...@@ -750,12 +747,11 @@ class ActiveRecord extends Model
*/ */
public function update($runValidation = true, $attributes = null) public function update($runValidation = true, $attributes = null)
{ {
if ($runValidation && !$this->validate($attributes)) { if ($runValidation && !$this->validate($attributes) || !$this->beforeSave(false)) {
return false; return false;
} }
if ($this->beforeSave(false)) {
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if ($values !== array()) { if (!empty($values)) {
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); $lock = $this->optimisticLock();
if ($lock !== null) { if ($lock !== null) {
...@@ -781,9 +777,6 @@ class ActiveRecord extends Model ...@@ -781,9 +777,6 @@ class ActiveRecord extends Model
} else { } else {
return 0; return 0;
} }
} else {
return false;
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment