I'm working on a program that is a reservation service for lab systems using Yii2.0. I've been using Yii for a little while but for some reason this one is stumping me
I have all of the systems listed in a gridview and I'd like to have a button in the actionColumn that will run the 'reserve' action and then show the view for the individual system.
I have the button added and it brings the user to the view page but I don't know what I need to do to have it run the reserve action first...or if it's even possible. I've tried pointing it to the reserve action in the controller but of course that looks for a view page rather than an action.
Here's some of the code from what I've been trying after looking through many pages of suggestions:
On the index page
['class' => 'yii\grid\ActionColumn',
'template' => '{reserve}',
'buttons' => [
'reserve' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-ok-circle"></span>',
$url,
[
'title' => 'Reserve',
'data-method' => 'post',
'data-pjax' => 0,
]);
}
],
'urlCreator' => function ($action, $model, $key, $index) {
return Url::toRoute(['cml/view', 'id' => $key]);
}
Function in Controller
public function actionReserve($id)
{
$model = $this->findModel($id);
if ($model->fkReservedTo == 1)
{
$model->fkReservedTo = Yii::$app->user->id;
// shouldn't you call save here
$model->save();
return $this->redirect(['view','id'=>$id]);
}
else
{
Yii::$app->session->setFlash('error', 'This system is not available to be reserved');
return $this->showAlert();
}
}
Any suggestions would be appreciated.