Browse Source

Fixes #16252: Fixed `yii\base\DynamicModel` for checking exist property

tags/2.0.16
Vuong Minh 6 years ago committed by Alexander Makarov
parent
commit
aeeb6ce39d
  1. 2
      framework/CHANGELOG.md
  2. 38
      framework/base/DynamicModel.php
  3. 4
      tests/framework/base/DynamicModelTest.php

2
framework/CHANGELOG.md

@ -27,12 +27,12 @@ Yii Framework 2 Change Log
- Bug #16277: Fixed `yii\db\Query::from()` to respect `yii\db\ExpressionInterface` (noname007)
- Bug #16280: Fixed `yii\base\Model::getActiveValidators()` to return correct validators for attribute on scenario (paweljankowiak06)
- Enh #16191: Enhanced `yii\helpers\Inflector` to work correctly with UTF-8 (silverfire)
- Bug #16252: Fixed `yii\base\DynamicModel` for checking exist property (vuongxuongminh)
- Bug: Fixed bad instanceof check in `yii\db\Schema::getTableMetadata()` (samdark)
- Bug #16292: Fixed misconfigured CORS filter exception throwing. Now it throws `InvalidConfigException` in Debug mode (khvalov)
- Bug #16301: Fixed `yii\web\User::setIdentity()` to clear access check cache while setting identity object to `null` (Izumi-kun)
- Bug #16322: Fixed strings were not were not compared using timing attack resistant approach while CSRF token validation (samdark, Felix Wiedemann)
2.0.15.1 March 21, 2018
-----------------------

38
framework/base/DynamicModel.php

@ -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,7 +116,7 @@ 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);
@ -124,6 +124,32 @@ class DynamicModel extends Model
}
/**
* {@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
* @param mixed $value the attribute value
@ -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.');

4
tests/framework/base/DynamicModelTest.php

@ -72,6 +72,10 @@ class DynamicModelTest extends TestCase
$model = new DynamicModel(compact('name', 'email'));
$this->assertEquals($email, $model->email);
$this->assertEquals($name, $model->name);
$this->assertTrue($model->canGetProperty('email'));
$this->assertTrue($model->canGetProperty('name'));
$this->assertTrue($model->canSetProperty('email'));
$this->assertTrue($model->canSetProperty('name'));
$this->expectException('yii\base\UnknownPropertyException');
$age = $model->age;
}

Loading…
Cancel
Save