简体   繁体   English

Yii:在 createCommand 执行查询后获取 id

[英]Yii : Getting id after createCommand execute query

In active record, I usually just get在活动记录中,我通常只会得到

$model->id, 

after

executing $model->save();

But how about createCommand?但是 createCommand 呢? how do I get the id (pk) of the inserted row?如何获取插入行的 id (pk)?

Yii::app()->db->createCommand($sql)->query();

I tried: $id = Yii::app()->db->getLastInsertedID();我试过: $id = Yii::app()->db->getLastInsertedID();

but it asks for sequence name.但它要求序列名称。 error.错误。 How do I do the active record counter part?我如何做活动记录计数器部分? Thanks!谢谢!

If you go with execute() instead of query()如果你使用execute()而不是query()

 Yii::app()->db->createCommand($sql)->execute();

then you can use然后你可以使用

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

to get the Id of the newly inserted record.获取新插入记录的 ID。

You can read more here .您可以在此处阅读更多内容。

Insert row need execute插入行需要执行

try:尝试:

Yii::app()->db->createCommand($sql)->execute()

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

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