How to get last insert record - Yii 2

12.8k views Asked by At

I am making application to create barcode in my application, i want to create Barang but in issue slip i want issue slip filled automatically from the last issue slip what can i do to fix it?

enter image description here

this is my view

<?php

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

/* @var $this yii\web\View */
/* @var $model backend\models\Barang */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="barang-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'rm_code')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'nama_barang')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'berat')->textInput(['maxlength' => true]) ?>



    <?= $form->field($model, 'issue_slip')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

 <div class="row">
    <?= $form->field($model, 'berat')->textInput(['maxlength' => true]) ?>
    <?php echo  \Yii::$app->db->getLastInsertId('{{barang}}');?>

  </div>
2

There are 2 answers

5
Yasar Arafath On BEST ANSWER

In controller before render

public function actionCreate()
{
    $model = new Barang();
    $modelForSlip = Barang::find()->orderBy(['id'=> SORT_DESC])->one();
    $model->issue_slip = $modelForSlip->issue_slip;

    if ($model->load(Yii::$app->request->post())){

       $model->waktu = date('Y-m-d h:m:s');
       $model->user = \Yii::$app->user->identity->username;
       $model->save();

        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
         //$model->issue_slip= \Yii::$app->db->getLastInsertId();
    }
}
3
Fahad Ali On

Let say this is your create function:

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return $this->redirect(['update', 'id' => $model->Id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

then redirect to new page with last inserted ID Now make you update function like this

 public function actionUpdate($id)
        {
            $model2 = Model::findOne($id);
            $model = new Modal();
 $model->issue_slip = $model2['issue_slip'];




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

In YII2 you can get last inserted record after save() function easily

like

 $model->save();
echo $model->Id 

or whatever column you want to get instead of issue_slip you can use whatever column you want