mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-11 19:20:01 +08:00
ActiveRecord::insert() and ActiveRecord::update() refactoring
This commit is contained in:
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user