Browse Source

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

ActiveRecord::insert() and ActiveRecord::update() refactoring
tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
1c93c50386
  1. 15
      framework/db/ActiveRecord.php

15
framework/db/ActiveRecord.php

@ -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
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
*/ */
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
} else { } else {
return 0; return 0;
} }
} else {
return false;
}
} }
/** /**

Loading…
Cancel
Save