H
H
hollanditkzn2017-04-05 13:56:19
Yii
hollanditkzn, 2017-04-05 13:56:19

How to make a form in one controller to add to another database table?

I have a main one where orders are stored and in the full view of the order, they are stored in the Zakaz table, on this page there is a delivery button. clicking on it opens a modal window. There is a form in the modal window that should save data to the Courier table.
I sketched an example as described in the documentation https://yiiframework.com.ua/ru/doc/guide/2/input-m...
Here is how I organized it in the controller

public function actionView($id)
    {
        $model = $this->findModel($id);
        $shipping = $this->findShipping($id);

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

        if ($model->load(Yii::$app->request->post())) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if(isset($model->file))
            {
            $model->file->saveAs('maket/Maket_'.$model->id_zakaz.'.'.$model->file->extension);
            $model->maket = 'Maket_'.$model->id_zakaz.'.'.$model->file->extension;
            $model->status = 4;
            }            
            $model->save();

            return $this->redirect(['view', 'id' => $model->id_zakaz]);
        } 
        return $this->render('view', [
            'model' => $this->findModel($id),
            'user_name' => $user_name,
            'shipping' => $shipping,
            'file' => $file,
        ]);
    }
protected function findModel($id)
    {
        if (($model = Zakaz::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    protected function findShipping($id)
    {
        if (($shipping = Courier::findOne($id)) !== null) {
            return $shipping;
        } else {
            throw new NotFoundHttpException("The requested page does not exist.");
            
        }
    }

In zakaz/view is the following code
<?php Modal::begin([
            'header' => '<h2>Доставка<h2>',
            'size' => 'modal-lg',
            'toggleButton' => [
                'tag' => 'button',
                'class' => 'btn btn-info btn-sm',
                'label' => 'Доставка',
            ],
        ]);


        echo $this->render('shipping', ['shipping' => $shipping]); 

        Modal::end(); ?>

And ate in the shipping file
<?php 

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<div class="zakaz-shippingForm">
  <?php $f = ActiveForm::begin(); ?>

  <?= $f->field($shipping, 'id_zakaz')->textInput() ?>
  <?php ActiveForm::end(); ?>
</div>

I now have a page with an error
Not Found (#404)
The requested page does not exist
How to write correctly and where did I make a mistake?
An easy way, of course, is to display this form on a separate page, but of course I would like to do it in the modal

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-05
@hollanditkzn

Some kind of hell, both in terms of the code and the description of the issue. Let's start with the most likely:

$model = $this->findModel($id);
$shipping = $this->findShipping($id);

I strongly doubt that both there and there the same id. As a result, one of these lines throws:
throw new NotFoundHttpException("The requested page does not exist.");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question