mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-20 06:08:08 +01:00
merge from yiisoft/yii2
This commit is contained in:
@@ -80,7 +80,7 @@ class DynamicModel extends Model
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->_attributes)) {
|
||||
if ($this->hasAttribute($name)) {
|
||||
return $this->_attributes[$name];
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ class DynamicModel extends Model
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if (array_key_exists($name, $this->_attributes)) {
|
||||
if ($this->hasAttribute($name)) {
|
||||
$this->_attributes[$name] = $value;
|
||||
} else {
|
||||
parent::__set($name, $value);
|
||||
@@ -104,7 +104,7 @@ class DynamicModel extends Model
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->_attributes)) {
|
||||
if ($this->hasAttribute($name)) {
|
||||
return isset($this->_attributes[$name]);
|
||||
}
|
||||
|
||||
@@ -116,13 +116,39 @@ class DynamicModel extends Model
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->_attributes)) {
|
||||
if ($this->hasAttribute($name)) {
|
||||
unset($this->_attributes[$name]);
|
||||
} else {
|
||||
parent::__unset($name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
|
||||
{
|
||||
return parent::canGetProperty($name, $checkVars, $checkBehaviors) || $this->hasAttribute($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
|
||||
{
|
||||
return parent::canSetProperty($name, $checkVars, $checkBehaviors) || $this->hasAttribute($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value indicating whether the model has an attribute with the specified name.
|
||||
* @param string $name the name of the attribute
|
||||
* @return bool whether the model has an attribute with the specified name
|
||||
*/
|
||||
public function hasAttribute($name)
|
||||
{
|
||||
return array_key_exists($name, $this->_attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an attribute.
|
||||
* @param string $name the attribute name
|
||||
@@ -155,7 +181,7 @@ class DynamicModel extends Model
|
||||
public function addRule($attributes, $validator, $options = [])
|
||||
{
|
||||
$validators = $this->getValidators();
|
||||
$validators->append(Validator::createValidator($validator, $this, (array) $attributes, $options));
|
||||
$validators->append(Validator::createValidator($validator, $this, (array)$attributes, $options));
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -179,7 +205,7 @@ class DynamicModel extends Model
|
||||
if ($rule instanceof Validator) {
|
||||
$validators->append($rule);
|
||||
} elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
|
||||
$validator = Validator::createValidator($rule[1], $model, (array) $rule[0], array_slice($rule, 2));
|
||||
$validator = Validator::createValidator($rule[1], $model, (array)$rule[0], array_slice($rule, 2));
|
||||
$validators->append($validator);
|
||||
} else {
|
||||
throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
|
||||
|
||||
Reference in New Issue
Block a user