D
D
dblearn942019-11-11 15:03:29
Yii
dblearn94, 2019-11-11 15:03:29

Tabular input with related data?

there are 2 tables:
option - id, name
page_option - id, option_id, lang_id,
value

public function getPageOption()
    {
        return $this->hasOne(PageOption::className(), ['option_id' => 'id']);
    }

Everything finds, everything works, but I can’t figure out how to work with the form if we have a tabular input?
here is the form:
.. active form begin
foreach ($options as $index => $option) {
    echo $form->field($option, "[$index]pageOption[value]")->label($option->name);
}
.. active form end

It outputs, but only the id and name of all these fields are the same: pageOption[value]
and how to work with this is not clear
UPD, we figured it out, now the problem is saving the associated data:
controller/action:
public function actionUpdate()
    {
        $options = Options::find()->with('pageOptions')->indexBy('id');
        
        if (Model::loadMultiple($options , \Yii::$app->request->post()) && Model::validateMultiple($options)) {
           
            foreach ($options  as $option) {
                $setting->save(false);
            }
        }
        
        return $this->render('index', compact("options"));
    }

Eventually:
Model::loadMultiple($options , \Yii::$app->request->post()) - true

but the related data has not been updated, it turns out that new data is not loaded into them
Please tell me, I'm clearly doing something wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-11-11
@dblearn94

if (Model::loadMultiple($options->pageOption , \Yii::$app->request->post()) && Model::validateMultiple($options->pageOption)) {
    foreach ($options->pageOption  as $page) {
                $page->save(false);
    }
}

ps Video to help .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question