Skip to content

Commit

Permalink
BaseActiveRecord methods canGetProperty() and canSetProperty()
Browse files Browse the repository at this point in the history
…improved to handle attribute list fetching failure.
  • Loading branch information
klimov-paul committed Sep 5, 2016
1 parent 87999bf commit ea03f56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 16 additions & 4 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,33 @@ public function optimisticLock()
*/
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if ($this->hasAttribute($name)) {
if (parent::canGetProperty($name, $checkVars, $checkBehaviors)) {
return true;
}
return parent::canGetProperty($name, $checkVars, $checkBehaviors);

try {
return $this->hasAttribute($name);
} catch (\Exception $e) {
// `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used
return false;
}
}

/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if ($this->hasAttribute($name)) {
if (parent::canSetProperty($name, $checkVars, $checkBehaviors)) {
return true;
}
return parent::canSetProperty($name, $checkVars, $checkBehaviors);

try {
return $this->hasAttribute($name);
} catch (\Exception $e) {
// `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used
return false;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/framework/db/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1300,11 +1300,16 @@ public function testAttributeAccess()

$this->assertTrue($model->canSetProperty('name'));
$this->assertTrue($model->canGetProperty('name'));
$this->assertFalse($model->canSetProperty('unExistingColumn'));
$this->assertFalse(isset($model->name));

$model->name = 'foo';
$this->assertTrue(isset($model->name));
unset($model->name);
$this->assertNull($model->name);

// @see https://github.com/yiisoft/yii2-gii/issues/190
$baseModel = new ActiveRecord();
$this->assertFalse($baseModel->hasProperty('unExistingColumn'));
}
}

0 comments on commit ea03f56

Please sign in to comment.