Answer the question
In order to leave comments, you need to log in
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.");
}
}
<?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(); ?>
<?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>
Answer the question
In order to leave comments, you need to log in
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);
throw new NotFoundHttpException("The requested page does not exist.");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question