PDO如何知道MySQL中最后插入的id?

发布于 2024-10-04 08:39:52 字数 1297 浏览 5 评论 0原文

编辑:这个问题的标题原来是:Doctrine如何知道MySQL中最后插入的id?并且与Doctrine ORM映射器相关。经过一番挖掘,我发现这个问题与Doctrine无关,而是与PDO_MySQLMySQL 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

压抑⊿情绪 2024-10-11 08:39:52

我做了一些研究并浏览了一些源代码,所以我的答案可能有点错误并且不准确。

首先,这与教义并没有真正的关系。 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 example INSERT), server responds with data and some other things included in OK Packet), including insert_id. So when we fire mysql_insert_id() we are not connecting to the server, to receive insert_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 file libmysql.c)

OK Packet is described here: MySQL Generic Response Packets - OK Packet

倚栏听风 2024-10-11 08:39:52

last_insert_id 仅适用于 AUTO_INCRMENT 列。如果您手动将值插入到 AUTO_INCRMENT 列中,它将不起作用。

该学说会起作用,因为它就像分配一个 NULL 给它(尽管你从来没有专门写过)。这会触发自动增量并为 last_insert_id() 提供一个值。

我在这里添加了一张 jpg 以便您可以看到。希望有帮助!

在此处输入图像描述

last_insert_id works only for AUTO_INCREMENT columns. It will not work if you insert a value manually into the AUTO_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 for last_insert_id().

I have included a jpg here so that you can see it. Hope it helps!

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文