在Yii中从MySQL获取最后插入的ID [英] Getting Last Inserted Id From MySQL in Yii

查看:233
本文介绍了在Yii中从MySQL获取最后插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图> _form.php文件中显示数据库的最后登录ID.我已经在_form.php文件中制作了这样的代码

I want to show the last login id from the database in view > _form.php file.I have made the code in _form.php file like this

  <div class="row">
    <?php echo $form->labelEx($model,'id'); ?>
    <?php echo Yii::app()->db->getLastInsertId('Form');?>
    <?php echo $form->error($model,'id'); ?>
  </div>

这里是表格和模型名称.但是我还是得到ID:0.哪里有错误的地方?

Here Form is the table and the model name.But Still I am getting ID:0.Where is the wrong part?

推荐答案

Pekka的答案对 common 很有用.但是,如果要在Yii Framework中执行该操作,请尝试以下操作:

Pekka's answer is good for common. But if you want to do that action in Yii Framework, try this:

$myModel = new $model;
$model -> savel(false);
echo $model->primaryKey; // Prints the last id.

或者您也可以尝试以下一般解决方案:

Or you may try this too for general solution:

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

最后,建议您查看

这篇关于在Yii中从MySQL获取最后插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆