简体   繁体   English

在Yii中获取最后插入的值

[英]Getting last inserted value in Yii

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. 我已经为Form创建了必要的CRUD。现在我想在视图文件中为最后一个插入的id获取一个额外的字段。那么如何获得该值?要获得该值,我应该在CRUD中进行任何必要的更改吗?任何帮助和建议都将是非常好看。

You can get the last inserted ID like this: 您可以像这样获取最后插入的ID:

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

See the Yii documentation for more information. 有关更多信息,请参阅Yii文档

如果您的目标是获取分配给您刚刚保存的模型的ID,那么在执行$model->save() ,只需执行$model->id即可将其恢复。

如果$model->id不起作用,则使用Yii::app()->db->getLastInsertId()getPrimaryKey()

you can also get the last inserted id of another model. 您还可以获取另一个模型的最后插入ID。

$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 你可以在同一个模型中持续插入id

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

In Yii2 last inserted id can be get using Yii2最后插入的id可以得到使用

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

added for the people looking for the answer of same question in yii2 yii2为寻找相同问题答案的人添加了

Reference Link : Here 参考链接: 这里

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

In Yii2 last inserted id can be get using 在Yii2中,最后插入的id可以得到使用

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

not use 不使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM