Skip to content

Commit dbcddc1

Browse files
authored
Merge pull request #11 from Olegf13/olegf13
Implement `ActiveRecord::populateRecord()` method.
2 parents 301156f + 21fde08 commit dbcddc1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/ActiveRecord.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use GuzzleHttp\Exception\ClientException;
1515
use yii\base\InvalidConfigException;
1616
use yii\base\NotSupportedException;
17+
use yii\base\UnknownPropertyException;
1718
use yii\db\BaseActiveRecord;
1819
use yii\helpers\Inflector;
1920
use yii\helpers\StringHelper;
@@ -235,4 +236,19 @@ public function unlinkAll($name, $delete = false)
235236
{
236237
throw new NotSupportedException('unlinkAll() is not supported by RestClient, use unlink() instead.');
237238
}
239+
240+
/**
241+
* @inheritdoc
242+
* @throws \yii\base\UnknownPropertyException
243+
*/
244+
public static function populateRecord($record, $row)
245+
{
246+
$attributes = array_flip($record->attributes());
247+
foreach ($attributes as $attributeName => $attributeValue) {
248+
if (!array_key_exists($attributeName, $row)) {
249+
throw new UnknownPropertyException("Attribute `{$attributeName}` not found in API response. Available fields: " . implode(', ', array_keys($row)) . '.');
250+
}
251+
}
252+
parent::populateRecord($record, $row);
253+
}
238254
}

0 commit comments

Comments
 (0)