dtueufe82643 2012-03-11 11:51
浏览 56
已采纳

克隆Dbal连接以在重复的Get_Affected_Rows上执行插入

I'm using Doctrine DBAL and wanting to do an INSERT ... ON DUPLICATE UPDATE where the return is affected_rows. Seems like you can't use the standard Dbal executeUpdate since INSERT ... ON DUPLICATE UPDATE and affected_rows are MySQL specific. That forces you to do a standard prepared SQL statement.

I'm using a Dependency Injection to insert the connection ( $this->connection ) into the class file. Should I be concerned about running an INSERT ... ON DUPLICATE UPDATE and then doing a 2nd qry to get the affected_rows under these conditions.

There doesn't seems to be a race situation since the DBAL connection is built up and broken down on each page request. I'm using the Symfony2 framework in this case but the answer should help you regardless of what framework OR no-framework, you're using.

  • 写回答

1条回答 默认 最新

  • douxiang3978 2012-03-15 12:57
    关注

    There's no reason to worry about Cloning an Object/Service in this situation. At least with the Symfony2 (SF2) framework, most Services only persist for the duration of the request. So my worry of affecting or intersecting a user experience on another request is a non-issue.

    But back to my INSERT .. ON DUPLICATE UPDATE issue, it's perfectly fine to do 2 queries back to back and there wont be any issues with race conditions since you're using the same connection to do both queries.

    The 2 Queries look like this

    $sql1 = "INSERT INTO table (...) VALUES (..) ON DUPLICATE KEY UPDATE ...";
    
    $sql2 = "SELECT ROW_COUNT()";
    
    // Expected Results for $sql2
    // 0 = no updates
    // 1 = new insert
    // 2 = update
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程