0

I need help building a simple protocol form with multiple attachments, where the user can change these attachments (files) later. That is, the attachments must have binding to a protocol.

(In this case I created 2 tables: mod_protocol and mod_files)

How should my code stay so that when I insert a new form, the attachments of this form are written to the mod_files table? (I'm not sure how to build a loop to write the files)

I'm at the beginning of this project, check out my files:

protocol form

<div class="protocol-form">

    <?php $form = ActiveForm::begin(); ?>

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

    <?= $form->field($model, 'description')->textarea(['rows' => 8]) ?>

    <?= $form->field($model, 'files[]')->fileInput(['multiple' => true,]) ?>


    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Save' : 'Save', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

protocol model

<?php

namespace app\modules\protocol\models;

use Yii;

class Protocol extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'mod_protocol';
    }

    public function rules()
    {
        return [
            [['name'], 'required'],
            [['name','description'], 'string', 'max' => 200],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Nome',
            'description' => 'Descrição'
        ];
    }

}

protocol controlller

public function actionCreate()
{
    $model = new Protocol();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

files model

<?php

namespace app\modules\protocol\models;

use Yii;

class Files extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'mod_files';
    }

    public function rules()
    {
        return [
            [['filename','protoco_id'], 'safe'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'filename' => 'Nome do Arquivo',
            'protoco_id' => 'Protocolo'
        ];
    }

}

1 Answer 1

1

So if file is an array:

use yourapp/model/ModFile;
...
public function actionCreate()
{
    $model = new Protocol();

    // I would write some logic, verify this saves first.
    foreach($model->files as $file) {
       $model_file = new ModFile();
       $model_file->file = $file;
       $model_file->save();
    }

    // If successful then save the main model.
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

In ActionCreate() of protocol controlller ??
Yes, in your controller
Another option is to place that logic in the mode with a beforeSave event. But get the above working first and then migrate to the event if your feeling brave.
Did this solution work for you? If so, can you select accept answer please. If not, how can I help you further?
Sorry for the delay, I've been traveling ... I'll try now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.