S
S
slip312019-06-02 15:39:58
Yii
slip31, 2019-06-02 15:39:58

Using Multiple Input, how to update?

Good afternoon. There are a lot of questions from me, but nothing can be done (
There are several models that are filled in in one form - An organization that can have several addresses, phone numbers and to which registration documents must be attached.
The Create method is fine, everything is filled in, everything is attached, everything preserved
5cf3c26798f9f424659479.png

public function actionCreate() {
        $model = new Organization(); 
        $modelOrganizationAddress = new OrganizationAddress();
        $modelOrganizationPhone = new OrganizationPhone();
        $license = new UploadLicenseForm();


        if ($model->load(Yii::$app->request->post()) &&
                           $modelOrganizationAddress->load(Yii::$app->request->post()) &&
                $modelOrganizationPhone->load(Yii::$app->request->post()) &&
                $license->load(Yii::$app->request->post()) &&
                $model->save()) {              
            $modelOrganizationAddress->organization_id = $model->id;
            $modelOrganizationPhone->organization_id = $model->id;
        
            $this->saveAddress($modelOrganizationAddress);
            $this->savePhone($modelOrganizationPhone);
            $license->licenseFiles = UploadedFile::getInstances($license, 'licenseFiles');
            if ($license->upload($modelOrganizationLang)) {
                Yii::$app->session->setFlash('success', "Организация сохранена");
            }

            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
                    'model' => $model,
                            'modelOrganizationAddress' => $modelOrganizationAddress,
                    'modelOrganizationPhone' => $modelOrganizationPhone,
                    'license' => $license
        ]);
    }

I would like to be able to edit it all with actionUpdate :
public function actionUpdate($id) {
        $model = $this->findModel($id);
          $modelOrganizationAddress = OrganizationAddress::find()->where(['organization_id' => $id])->all();
        $modelOrganizationPhone = OrganizationPhone::find()->where(['organization_id' => $id])->all();
                
      $license->scenario = 'update-photo-upload';
        if ($model->load(Yii::$app->request->post()) &&
                   $modelOrganizationAddress->load(Yii::$app->request->post()) &&
                $modelOrganizationPhone->load(Yii::$app->request->post()) &&
                $model->save()) {
//            $license->licenseFiles = UploadedFile::getInstances($license, 'licenseFiles');
                     $modelOrganizationAddress->organization_id = $model->id;
            $modelOrganizationPhone->organization_id = $model->id;

         

            $this->saveAddress($modelOrganizationAddress);
            $this->savePhone($modelOrganizationPhone);
            $license->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($license->upload()) {
                   Yii::$app->session->setFlash('success', "Организация обновлена");
              
            }
            return $this->redirect(['view', 'id' => $model->id]);
        }

All this in my form, using Multiple Input:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->errorSummary($model); ?>
    <?=
    $form->field($model, 'status')->dropDownList([
        '0' => 'Отключена',
        '1' => 'Активна',
    ]);
    ?>
    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'full_name')->textInput(['maxlength' => true]) ?>



    <?php
    echo $form->field($modelOrganizationAddress, 'address')->label(HModule::t('organization', 'Address'))->widget(MultipleInput::className(), [
        'max' => 4,
        'columns' => [
            [
                'name' => 'country',
                'title' => HModule::t('organization', 'Country'),
                'options' => [
                    'class' => 'input-priority'
                ],
        ],
            [
                'name' => 'city',
                'title' => HModule::t('organization', 'City'),
                'options' => [
                    'class' => 'input-priority'
                ],
            
            ],
            [
                'name' => 'street',
                'title' => HModule::t('organization', 'Street'),
                'options' => [
                    'class' => 'input-priority'
                ]
            ],
            [
                'name' => 'building',
                'title' => HModule::t('organization', 'Building'),
                'options' => [
                    'class' => 'input-priority'
                ]
            ],
            [
                'name' => 'zip_code',
                'title' => HModule::t('organization', 'Zip Code'),
                'options' => [
                    'class' => 'input-priority'
                ]
            ]
        ]
    ]);
    ?>
    ...
     <?php echo $form->field($license, 'licenseFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

Question - how does update work with multiple filled fields (multiple)?
I have to pass in _form all the addresses belonging to the organization:
Country1, Street2
Country2, Street2
...
But how to do it, because there is only one field?)
echo $form->field($modelOrganizationAddress, 'address')->label(HModule::t('organization', 'Address'))->widget(MultipleInput::className(), [
        'max' => 4,
        'columns' => [
            [
                'name' => 'country',
                'title' => HModule::t('organization', 'Country'),
                'options' => [
                    'class' => 'input-priority'
                ],

            ],
            [
                'name' => 'city',
                'title' => HModule::t('organization', 'City'),
                'options' => [
                    'class' => 'input-priority'
                ],
            
            ],
        ...
            [
                'name' => 'zip_code',
                'title' => HModule::t('organization', 'Zip Code'),
                'options' => [
                    'class' => 'input-priority'
                ]
            ]
        ]
    ]);

How is it necessary to pass some kind of $items array to multiply?
I confess that maybe this is in the documentation , but it’s not entirely clear to me how the already filled values ​​“get” into the form:
Call to a member function formName() on array, well, that’s how I get yes, an array of objects
PS I hope that they don’t get banned on the toaster for duplicating questions on different resources

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question