Skip to content

Commit

Permalink
Merge pull request ruskid#14 from shurik2k5/master
Browse files Browse the repository at this point in the history
Add virtual attributes to ARImportStrategy
  • Loading branch information
ruskid authored May 23, 2019
2 parents 40adb0d + 5efa1cf commit 2c9a55b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ARImportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
]
],
]));
Expand Down

0 comments on commit 2c9a55b

Please sign in to comment.