Fixed canGetProperty() and canSetProperty() returns false for yii\db\BaseActiveRecord attributes

This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
Klimov Paul
2016-08-30 17:39:23 +03:00
gitea-unlock(16/)
parent cd26cbf11a
commit 75439d367d
octicon-diff(16/tw-mr-1) 3 changed files with 38 additions and 1 deletions

1
framework/CHANGELOG.md
View File

@@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Enh #9989: ActiveForm now respects formtarget, formmethod and formenctype attributes of submit button (AnatolyRugalev)
- Enh #12296: Added value validation to `yii\log\Target::setLevels()` (Mak-Di)
- Enh #12073: Added the ability to suppress the generation of input hint when it is specified through `Model::attributeHints()` (PowerGamer1)
- Bug #9561: Fixed `canGetProperty()` and `canSetProperty()` returns `false` for `yii\db\BaseActiveRecord` attributes (klimov-paul)
- Bug #11990: Fixed `yii\db\BaseActiveRecord::refresh()` may set incorrect `oldAttributes` values at some cases (only-victor)
- Bug #12009: Do not render "for" field label attribute for active form RadioList and CheckboxList (shevchik87, samdark)
- Bug #12068: Added missing 'LEVEL_PROFILE' for the syslog target (Mak-Di)

22
framework/db/BaseActiveRecord.php
View File

@@ -230,6 +230,28 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
return null;
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if ($this->hasAttribute($name)) {
return true;
}
return parent::canGetProperty($name, $checkVars, $checkBehaviors);
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if ($this->hasAttribute($name)) {
return true;
}
return parent::canSetProperty($name, $checkVars, $checkBehaviors);
}
/**
* PHP getter magic method.
* This method is overridden so that attributes and related objects can be accessed like properties.

16
tests/framework/db/ActiveRecordTest.php
View File

@@ -1293,4 +1293,18 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertTrue($newOrder->getIsNewRecord());
$this->assertEquals($newTotal, $newOrder->total);
}
}
public function testAttributeAccess()
{
$model = new Customer();
$this->assertTrue($model->canSetProperty('name'));
$this->assertTrue($model->canGetProperty('name'));
$this->assertFalse(isset($model->name));
$model->name = 'foo';
$this->assertTrue(isset($model->name));
unset($model->name);
$this->assertNull($model->name);
}
}