【问题标题】:Getting ids of inserted raws via doctrine dbal通过学说 dbal 获取插入的原始数据的 id
【发布时间】:2017-11-25 01:24:56
【问题描述】:

我正在开发一个 symfony 应用程序,我需要一次插入多个原始数据,Doctrine ORM 不是一个好的选择,因为对于每个原始数据,它将打开一个连接来执行查询,以避免这种情况并有一个连接插入所有我使用准备好的学说 dbal 语句的原始数据,它工作正常,除了我需要获取插入原始数据的 id,似乎唯一可用的函数是 lastinsertedid,它只返回最后一个 id,而不是所有最后插入的,如何我能做到这一点吗? 任何帮助,将不胜感激!

【问题讨论】:

  • 有点离题,但 Doctrine ORM 为每个查询打开一个新连接的概念并不准确。可以展示一下你准备好的sql语句吗?

标签: symfony doctrine dbal


【解决方案1】:

这其实和教义一点关系都没有。如果你想要所有插入的 id,它必须在 MySQL 中是可能的。 “如果学说没有批量插入,它不太可能支持批量插入后返回 id 列表:)”

查看与MYSQL相关的答案:

How can I Insert many rows into a MySQL table and return the new IDs?

MySQL LAST_INSERT_ID() used with multiple records INSERT statement

但是在 postgresql 中是可能的(因为你没有提到你的 DB):

Retrieving serial id from batch inserted rows in postgresql

【讨论】:

    【解决方案2】:

    您实际上可以在将内容插入数据库之前生成 ID。例如,使用随机 UUID。 这个库可能有用:https://github.com/ramsey/uuid

    use Ramsey\Uuid\Uuid;
    
    $uuid4 = Uuid::uuid4();
    echo $uuid4->toString()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2019-07-30
      • 1970-01-01
      • 2013-05-31
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多