Getting last inserted value in Yii

I already made a model for the form. The fact that the fields were like

id
firstname
lastname
description
created_at
updated_at
created_by
updated_by

I made the necessary CRUD for the form. Now I want to get one additional field for the last inserted id in the view.So file, how to get this value? To get this value, do I have to make any necessary changes to CRUD? Any help and suggestions would be very helpful.

+6
source share
7 answers

You can get the last inserted id, for example:

Yii::app()->db->getLastInsertId();

See the Yii documentation for more information .

+22
source

- , , $model->save() $model->id .

+3

$model->id , Yii::app()->db->getLastInsertId() getPrimaryKey().

+2

.

$std_id = Students::model()->findAll(array('order' => 'admission_no DESC','limit' => 1));

            foreach($std_id as $f) {

                echo  "Last Inserted Admission No:".$f['admission_no'];
                }

id

Yii::app()->db->getLastInsertID();
+2

Yii2

Yii::$app->db->getLastInsertedID();

, Yii2

+2

:

$model->primaryKey or
$model->id // this is your primary key item
0

Yii2

Yii::$app->db->getLastInsertID();

Yii::$app->db->getLastInsertedID();
0

All Articles