简体   繁体   English

如何在ZF2中检索插入行的ID?

[英]How to retrieve the id of an inserted row in ZF2?

I am creating a message system in ZF2. 我正在ZF2中创建一个消息系统。

I have a threads table: 我有一个线程表:

id             INT primary key auto_increment
title          TEXT

And a messages table: 和一个消息表:

message_id     INT PRIMARY KEY auto_increment
thread_id      INT foreign key references threads.id
message        TEXT

(Rows and other referenced tables were left out which are not entirely pertinent to this question.) (省略了与该问题不完全相关的行和其他引用的表。)

In my MessageTable model, it keeps an instance of the ThreadTable. 在我的MessageTable模型中,它保留ThreadTable的一个实例。 When MessageTable::saveMessage( Message $message ) is called it creates a Thread model and in turn calls ThreadTable::saveThread( Thread $thread ). 调用MessageTable :: saveMessage(Message $ message)时,它将创建一个Thread模型,然后依次调用ThreadTable :: saveThread(Thread $ thread)。

Since my Message model requires the thread_id, it needs to know the threads.id we just inserted before I can insert the new message. 由于我的消息模型需要thread_id,因此在插入新消息之前,它需要知道我们刚刚插入的threads.id。

I do not know how to retrieve the id that we just inserted. 我不知道如何检索刚插入的ID。 How would I get the last inserted id? 我将如何获取最后插入的ID?

$id = $adapter->getDriver()->getLastGeneratedValue();

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

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