1

i have condition that has been bothering me sometimes. i have two models sewalocker and loker, these two models are related. i have a form in sewalockers that required to fill locker number as loker_id. when the form is submitted, i want status of selected loker_id assigned as 'loaned' not 'available' any more in loker table. how do i do that?

here is my sewaController :

<?php

namespace app\controllers;

use Yii;
use app\models\SewaLocker;
use app\models\sewaSearch;
use app\models\loker;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;


class SewaController extends Controller
{
 public function behaviors()
 {
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}

/**
 * Lists all SewaLocker models.
 * @return mixed
 */
public function actionIndex()
{
    $searchModel = new sewaSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

/**
 * Displays a single SewaLocker model.
 * @param integer $id
 * @return mixed
 */
public function actionView($id)
{
    return $this->render('view', [
        'model' => $this->findModel($id),
    ]);
}

/**
 * Creates a new SewaLocker model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 * @return mixed
 */
public function actionCreate()
{
    $model = new SewaLocker();
    $loker = new loker();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id_sewa]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}
2
  • You want to do this in create action? .. Commented Jan 22, 2016 at 15:38
  • yes i want to do it in actionCreate() in SewaController Commented Jan 22, 2016 at 16:09

1 Answer 1

1

In create you should do this way

if the SewaLocker model is saved you then can retrive the right loker model adn update..

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


    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $loker = $this->findLokerModel($model->locker_id);
        $loker->status ="Loaned not available";
        $loker->save();

        return $this->redirect(['view', 'id' => $model->id_sewa]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

protected function findLokerModel($id)
{
    if (($model = Loker::findOne($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

it says 'Call to undefined function app\controllers\findLokermodel()' . what i am missing then?
You added the findLoker model in your controller ? eventually change protected in public for test..
yes i added in my controller. nothing's changed, it says the same.
$loker = $this->findLokerModel($model->locker_id);

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.