Open jomonkj opened 9 years ago
Example for single standard model:
[
'className' => Question::className(),
'labels' => ['Question', 'Questions'],
'setScenario' => true,
],
Scenario that will be set is arogachev\excel\import\basic\StandardModel::SCENARIO_IMPORT
. Alternatively you can use arogachev\excel\import\advanced\StandardModel::SCENARIO_IMPORT
. You can refer to it in model rules like this:
['test_id', 'required', 'on' => StandardModel::SCENARIO_IMPORT],
Sorry, the docs are not very clear. Need to update it with such details.
but 'setScenario' => true,
only set in
protected static $_setScenarioEvents = [
ActiveRecord::EVENT_INIT,
ActiveRecord::EVENT_AFTER_FIND,
];
it conflict with UploadBehavior i am using
And what's wrong with it? It's only needed when model is created or found.
i am using https://github.com/trntv/yii2-file-kit to upload file. It have UploadBehavior for image_path, image_base_url fields in default scenario. i want to update that fields with csv data. i want to write code like this
public function behaviors()
{
$behaviors = [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
],
];
// exit($this->scenario);
if ($this->scenario != 'import') {
$behaviors = array_merge($behaviors, [
[
'class' => UploadBehavior::className(),
'attribute' => 'image',
'pathAttribute' => 'image_base_url',
'baseUrlAttribute' => 'image_base_url',
],
]);
}
return $behaviors;
}
when echoing scenario i am getting default
scenario. even if setScenario' => true
In provided code I don't see the usage of importer at all.
controller import action
public function actionImport()
{
$model = new ImportForm();
$model->type = 'series';
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->upload()) {
$importer = new Importer([
'filePath' => $model->getFile(),
'standardModelsConfig' => [
[
'className' => ProductSeries::className(),
'setScenario' => true,
'standardAttributesConfig' => [
['name' => 'name',],
['name' => 'slug',],
['name' => 'description',],
['name' => 'image_path',],
['name' => 'image_path',],
['name' => 'meta_title',],
['name' => 'meta_description',],
['name' => 'meta_keywords',],
['name' => 'status',],
],
],
],
]);
if (!$importer->run()) {
echo $importer->error;
if ($importer->wrongModel) {
echo Html::errorSummary($importer->wrongModel);
}
}
}
}
i think
protected static $_setScenarioEvents
need to public
Where the code with behaviors
is placed? This scenario is needed for setting scenario of imported models.
in ProductSeries model
So behaviors are attached eariler than ActiveRecord::EVENT_INIT
event is triggered? Weird. Need to test it.
yes
Need import scenario
ActiveRecord::EVENT_BEFORE_INSERT, ....