Reputation: 261
In short, I have the following code:
<?= $form->field( $isntmodel, 'id')->textInput() ?>
<?= Html::a('<i class="mdi-action-done"></i>', ['add-item', 'id' => ???], [
'class' => 'btn-btn-add pull-right',
]) ?>
This code is wrong.
So. I need get value which will input by user. And set it instead ???
$form->field()
must have $model, $attribute, $options = []
. How can I write field not using $model and $attribute? It is not table column, I need just get value and set it instead ???
I try that
public function actionAddItem($id) {
$model = $this->$model;
$product = Product::findOne($id);
$orderItem = new OrderItem();
$orderItem->order_id = $model->id;
$orderItem->title = $product->title;
$orderItem->price = $product->getPrice();
$orderItem->product_id = $product->id;
$orderItem->save();
return $this->redirect(['index']);
}
But it throws an exception. At row with $model = $this->$model
, and I don't know how from field submit id to link
Adding item is work if i put this in browser http://yii2-shop/backend/web/order/add-item?modelid=13&id=1&quantity=4
UPD
Now my form looks like
<?php $form = ActiveForm::begin([]); ?>
<tr>
<td><?= $n ?></td>
<td><?= $form->field( $model, 'newOrderItemId')->textInput()->label(false) ?></td>
<td></td>
<td></td>
<td class="text-center"><?= $form->field( $model, 'newOrderItemQuantity')->textInput()->label(false) ?></td>
<td>
<?= Html::a('<i class="mdi-action-done"></i>', [
'/order/add-item',
'modelid' => $model->id,
'id' => $model->newOrderItemId,
'quantity' => $model->newOrderItemQuantity,
], [
'class' => 'btn btn-add pull-right',
'data-toggle'=>'tooltip' ,
'data-placement'=>'bottom',
'title'=>'Добавить товар',
]) ?>
</td>
</tr>
<?php ActiveForm::end(); ?>
And add-item
looks like
public function actionAddItem($modelid, $id, $quantity) {
$model = $this->findModel($modelid);
$product = Product::findOne($id);
$orderItem = new OrderItem();
$orderItem->order_id = $model->id;
$orderItem->title = $product->title;
$orderItem->price = $product->getPrice();
$orderItem->product_id = $product->id;
$orderItem->quantity = $quantity;
$orderItem->save();
return $this->redirect(['index']);
}
newOrderItemId
and newOrderItemQuantity
are just public variables which I mark at Order
model. I can't get form field value for submit it to add-item
Upvotes: 4
Views: 5830
Reputation: 261
So. I solved the problem.
I created AddOrderItem
model for announce variables
<?php namespace backend\models;
use yii\base\Model;
class AddOrderItem extends Model {
public $modelid;
public $id;
public $quantity;
public function rules() {
return [
[['modelid','id','quantity'], 'integer'],
];
}
}
And I edited actionUpdate()
now it's looks like
public function actionUpdate($id) {
$model = $this->findModel($id);
$addOrderModel = new AddOrderItem();
if ($addOrderModel->load(Yii::$app->request->post())) {
$product = Product::findOne($addOrderModel->id);
$orderItem = new OrderItem();
$orderItem->order_id = $model->id;
$orderItem->title = $product->title;
$orderItem->price = $product->getPrice();
$orderItem->product_id = $product->id;
$orderItem->quantity = $addOrderModel->quantity;
$orderItem->save();
return $this->redirect(['view', 'id' => $model->id]);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
'addOrderModel' => $addOrderModel
]);
}
}
At views/order/update
i added following row
<?= $this->render('_addItemForm', ['model' => $addOrderModel]); ?>
And _addItemForm
now contains this:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin(); ?>
<td><?= $form->field( $model , 'id')->textInput()->label(false) ?></td>
<td></td>
<td></td>
<td class="text-center"><?= $form->field( $model , 'quantity')->textInput()->label(false) ?></td>
<td>
<?= Html::submitButton('<i class="mdi-action-done"></i>',[
'class' => 'btn btn-add pull-right',
'data-toggle'=>'tooltip' ,
'data-placement'=>'bottom',
'title'=>'Добавить товар',
]) ?>
</td>
<?php ActiveForm::end(); ?>
I can't believe what I done it by myself. And I'm glad that I had no one to help, because now I know more.
Upvotes: 1