From 75439d367d2d1270bb61642f236df6a552acf29e Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 30 Aug 2016 17:39:23 +0300 Subject: [PATCH] Fixed `canGetProperty()` and `canSetProperty()` returns `false` for `yii\db\BaseActiveRecord` attributes --- framework/CHANGELOG.md | 1 + framework/db/BaseActiveRecord.php | 22 ++++++++++++++++++++++ tests/framework/db/ActiveRecordTest.php | 16 +++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 93be059dea..edd47a40c9 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 0e915fc92a..dda1920c43 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -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. diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 7cf2682eb9..c07ba29340 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -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); + } +} \ No newline at end of file