Getting last inserted value in Yii

2020-07-11 06:50发布

I have already made a model for form.In that the fields were like

id
firstname
lastname
description
created_at
updated_at
created_by
updated_by

I have made necessary CRUD for Form.Now I want to get one extra field for last inserted id in view file.So how to get that value?To get that value should I make any necessary changes in CRUD?Any help and suggestions will be highly appriciable.

标签: php mysql yii
7条回答
萌系小妹纸
2楼-- · 2020-07-11 07:22

you can also get the last inserted id of another model.

$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'];
                }

or

you can last inserted id in the same model

Yii::app()->db->getLastInsertID();
查看更多
我命由我不由天
3楼-- · 2020-07-11 07:24

If $model->id wouldn't work then use Yii::app()->db->getLastInsertId() or getPrimaryKey().

查看更多
太酷不给撩
4楼-- · 2020-07-11 07:25

In Yii2 last inserted id can be get using

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

not use

Yii::$app->db->getLastInsertedID();
查看更多
The star\"
5楼-- · 2020-07-11 07:30

If your goal is to get the id that was assigned to the model that you just saved, then after you do $model->save(), simply do $model->id to get it back.

查看更多
神经病院院长
6楼-- · 2020-07-11 07:38

Reference Link : Here

$model->primaryKey or
$model->id // this is your primary key item
查看更多
对你真心纯属浪费
7楼-- · 2020-07-11 07:42

You can get the last inserted ID like this:

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

See the Yii documentation for more information.

查看更多
登录 后发表回答