mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-23 12:10:54 +08:00
made AR attribute manipulation behave consistent to hasAttribute()
This commit is contained in:
@ -103,7 +103,7 @@ class ActiveRecord extends Model
|
|||||||
/**
|
/**
|
||||||
* @var array related models indexed by the relation names
|
* @var array related models indexed by the relation names
|
||||||
*/
|
*/
|
||||||
private $_related;
|
private $_related = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -376,11 +376,11 @@ class ActiveRecord extends Model
|
|||||||
{
|
{
|
||||||
if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
|
if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
|
||||||
return $this->_attributes[$name];
|
return $this->_attributes[$name];
|
||||||
} elseif (isset($this->getTableSchema()->columns[$name])) {
|
} elseif ($this->hasAttribute($name)) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
$t = strtolower($name);
|
$t = strtolower($name);
|
||||||
if (isset($this->_related[$t]) || $this->_related !== null && array_key_exists($t, $this->_related)) {
|
if (isset($this->_related[$t]) || array_key_exists($t, $this->_related)) {
|
||||||
return $this->_related[$t];
|
return $this->_related[$t];
|
||||||
}
|
}
|
||||||
$value = parent::__get($name);
|
$value = parent::__get($name);
|
||||||
@ -430,7 +430,7 @@ class ActiveRecord extends Model
|
|||||||
*/
|
*/
|
||||||
public function __unset($name)
|
public function __unset($name)
|
||||||
{
|
{
|
||||||
if (isset($this->getTableSchema()->columns[$name])) {
|
if ($this->hasAttribute($name)) {
|
||||||
unset($this->_attributes[$name]);
|
unset($this->_attributes[$name]);
|
||||||
} else {
|
} else {
|
||||||
$t = strtolower($name);
|
$t = strtolower($name);
|
||||||
@ -541,6 +541,16 @@ class ActiveRecord extends Model
|
|||||||
return array_keys($this->getTableSchema()->columns);
|
return array_keys($this->getTableSchema()->columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value indicating whether the model has an attribute with the specified name.
|
||||||
|
* @param string $name the name of the attribute
|
||||||
|
* @return boolean whether the model has an attribute with the specified name.
|
||||||
|
*/
|
||||||
|
public function hasAttribute($name)
|
||||||
|
{
|
||||||
|
return isset($this->_attributes[$name]) || isset($this->getTableSchema()->columns[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the named attribute value.
|
* Returns the named attribute value.
|
||||||
* If this record is the result of a query and the attribute is not loaded,
|
* If this record is the result of a query and the attribute is not loaded,
|
||||||
@ -570,16 +580,6 @@ class ActiveRecord extends Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a value indicating whether the model has an attribute with the specified name.
|
|
||||||
* @param string $name the name of the attribute
|
|
||||||
* @return boolean whether the model has an attribute with the specified name.
|
|
||||||
*/
|
|
||||||
public function hasAttribute($name)
|
|
||||||
{
|
|
||||||
return isset($this->_attributes[$name]) || isset($this->getTableSchema()->columns[$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the old attribute values.
|
* Returns the old attribute values.
|
||||||
* @return array the old attribute values (name-value pairs)
|
* @return array the old attribute values (name-value pairs)
|
||||||
@ -622,7 +622,7 @@ class ActiveRecord extends Model
|
|||||||
*/
|
*/
|
||||||
public function setOldAttribute($name, $value)
|
public function setOldAttribute($name, $value)
|
||||||
{
|
{
|
||||||
if (isset($this->_oldAttributes[$name]) || isset($this->getTableSchema()->columns[$name])) {
|
if (isset($this->_oldAttributes[$name]) || $this->hasAttribute($name)) {
|
||||||
$this->_oldAttributes[$name] = $value;
|
$this->_oldAttributes[$name] = $value;
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".');
|
throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".');
|
||||||
@ -1138,7 +1138,7 @@ class ActiveRecord extends Model
|
|||||||
$this->_attributes[$name] = $record->_attributes[$name];
|
$this->_attributes[$name] = $record->_attributes[$name];
|
||||||
}
|
}
|
||||||
$this->_oldAttributes = $this->_attributes;
|
$this->_oldAttributes = $this->_attributes;
|
||||||
$this->_related = null;
|
$this->_related = [];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user