PDO如何知道MySQL中最后插入的id?
编辑:这个问题的标题原来是:Doctrine如何知道MySQL中最后插入的id?并且与Doctrine ORM映射器相关。经过一番挖掘,我发现这个问题与Doctrine无关,而是与PDO_MySQL、MySQL C API以及最后与MySQL客户端服务器相关沟通。我决定更改标题,这样也许有人会找到他/她的问题的答案。
对于那些不使用 Doctrine: 我很好奇,为什么下面:
mysql_query("INSERT INTO category (name) VALUES('cat')");
echo mysql_insert_id();
或类似:
$pdo->exec("INSERT INTO category (name) VALUES('cat')");
echo $pdo->lastInsertId();
只会导致日志中只有一个位置(没有单独的SELECT LAST_INSERT_ID()
):
1701 Query INSERT INTO category (name) VALUES ('cat')
原始问题:
我有 2 个表:
category(id,name)
product(id, name, categoryId)
我创建了新的类别对象和产品对象。我将类别对象分配给产品对象。我没有设置任何 id:
$product = new Product();
$product->name = 'asdf';
$category = new Category();
$category->name = 'cat';
$product->Category = $category;
之后我刷新了连接并检查 MySQL 日志:
1684 Query START TRANSACTION
1684 Query INSERT INTO category (name) VALUES ('cat')
1684 Query INSERT INTO product (name, categoryid) VALUES ('asdf', '312')
1684 Query COMMIT
Doctrine 如何知道新创建的类别 id 是 312?日志中没有其他内容。
EDIT: This question title originally was: How does Doctrine know last inserted id in MySQL? and was related to Doctrine ORM mapper. After some digging I found out that this question is not related to Doctrine but to PDO_MySQL, MySQL C API and finally - to MySQL client-server communication. I have decided to change the title, so maybe someone will find answer to his/hers question.
For those who are not using Doctrine: I was curious, why bellow:
mysql_query("INSERT INTO category (name) VALUES('cat')");
echo mysql_insert_id();
or similar:
$pdo->exec("INSERT INTO category (name) VALUES('cat')");
echo $pdo->lastInsertId();
will lead to only one position (without separate SELECT LAST_INSERT_ID()
) in log:
1701 Query INSERT INTO category (name) VALUES ('cat')
Original question:
I have 2 tables:
category(id,name)
product(id, name, categoryId)
I created new category object and product object. I assigned category object to product object. I didn't set any ids:
$product = new Product();
$product->name = 'asdf';
$category = new Category();
$category->name = 'cat';
$product->Category = $category;
After that I flushed the connection and check MySQL logs:
1684 Query START TRANSACTION
1684 Query INSERT INTO category (name) VALUES ('cat')
1684 Query INSERT INTO product (name, categoryid) VALUES ('asdf', '312')
1684 Query COMMIT
How did Doctrine know, that the newly created category id is 312? There is nothing else in logs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我做了一些研究并浏览了一些源代码,所以我的答案可能有点错误并且不准确。
首先,这与教义并没有真正的关系。 Doctrine 使用 PDO_MYSQL。但在内部PDO_MYSQL使用与
mysql_insert_id
函数相同的东西 - 原生MySQL C API函数 -mysql_insert_id
。没有单独的 SELECT LAST_INSERT_ID() 的原因在于,在执行该语句后(在我的示例中
INSERT
),服务器响应数据和其他一些内容OK 数据包中包含的内容),包括insert_id
。因此,当我们触发 mysql_insert_id() 时,我们不会连接到服务器来接收 insert_id - mysql 库不需要这样做 - 它已经从上次存储了这个值查询的执行(至少在分析文件libmysql.c
之后我是这么认为的)OK Packet在这里描述:MySQL 通用响应数据包 - OK 数据包
I did some research and browse some source code, so my answer could be a little bit wrong and not precise.
First of all, this is not really related to Doctrine. Doctrine uses PDO_MYSQL. But internally PDO_MYSQL uses the same thing as
mysql_insert_id
function - native MySQL C API function -mysql_insert_id
.The reason why there is no seperate
SELECT LAST_INSERT_ID()
lies in the fact, that after the statement is executed (in my exampleINSERT
), server responds with data and some other things included in OK Packet), includinginsert_id
. So when we firemysql_insert_id()
we are not connecting to the server, to receiveinsert_id
- mysql library does not need to do that - it already has this value stored from last execution of query (at least I think so after analyzing the filelibmysql.c
)OK Packet is described here: MySQL Generic Response Packets - OK Packet
可能通过调用 http://dev.mysql.com/ doc/refman/5.0/en/getting-unique-id.html
Probably by calling http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
last_insert_id
仅适用于AUTO_INCRMENT
列。如果您手动将值插入到AUTO_INCRMENT
列中,它将不起作用。该学说会起作用,因为它就像分配一个
NULL
给它(尽管你从来没有专门写过)。这会触发自动增量并为last_insert_id()
提供一个值。我在这里添加了一张 jpg 以便您可以看到。希望有帮助!
last_insert_id
works only forAUTO_INCREMENT
columns. It will not work if you insert a value manually into theAUTO_INCREMENT
column.The doctrine will work because it is just like assigning a
NULL
to it (although you never specifically write that). This triggers the auto increment and provide a value forlast_insert_id()
.I have included a jpg here so that you can see it. Hope it helps!