arogachev / yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework
Other
64 stars 25 forks source link

how to set scenario? #16

Open jomonkj opened 9 years ago

jomonkj commented 9 years ago

Need import scenario ActiveRecord::EVENT_BEFORE_INSERT, ....

arogachev commented 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],
arogachev commented 9 years ago

Sorry, the docs are not very clear. Need to update it with such details.

jomonkj commented 9 years ago

but 'setScenario' => true, only set in

 protected static $_setScenarioEvents = [
        ActiveRecord::EVENT_INIT,
        ActiveRecord::EVENT_AFTER_FIND,
    ];

it conflict with UploadBehavior i am using

arogachev commented 9 years ago

And what's wrong with it? It's only needed when model is created or found.

jomonkj commented 9 years ago

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

arogachev commented 9 years ago

In provided code I don't see the usage of importer at all.

jomonkj commented 9 years ago

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);
                    }
                }
            }
        }
jomonkj commented 9 years ago

i think protected static $_setScenarioEvents

need to public

arogachev commented 9 years ago

Where the code with behaviors is placed? This scenario is needed for setting scenario of imported models.

jomonkj commented 9 years ago

in ProductSeries model

arogachev commented 9 years ago

So behaviors are attached eariler than ActiveRecord::EVENT_INIT event is triggered? Weird. Need to test it.

jomonkj commented 9 years ago

yes