From 5efa1cf3440708c868ad728740b392648d878f42 Mon Sep 17 00:00:00 2001 From: Alex Pogorelov Date: Mon, 20 May 2019 01:15:59 +0300 Subject: [PATCH] Add virtual attributes to ARImportStrategy --- ARImportStrategy.php | 5 +++++ README.md | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/ARImportStrategy.php b/ARImportStrategy.php index 2d8dea3..e49de1b 100644 --- a/ARImportStrategy.php +++ b/ARImportStrategy.php @@ -71,6 +71,11 @@ public function import(&$data) { //Set value to the model $model->setAttribute($config['attribute'], $value); + } else + if (isset($config['virtual']) && $config['virtual']) { + //set value to virtual attribute + $value = call_user_func($config['value'], $row); + $model->{$config['attribute']} = $value; } } //Check if model is unique and saved with success diff --git a/README.md b/README.md index f88552d..5bab1a6 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,13 @@ $primaryKeys = $importer->import(new ARImportStrategy([ 'value' => function($line) { return $line[2]; }, + ], + [ + 'attribute' => 'items', + 'virtual' => true, //set non AR DB attribute (behavior or public model attribute) + 'value' => function($line) { + return Item::find()->select(['id'])->column('id'); + }, ] ], ]));