Pregunta

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.

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top