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 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动