开发者

doctrine2 native query update statement

How to do a native sql query in Doctrine 2, executing an update statement? The createNativeQ开发者_JAVA技巧uery method on EntityManager, requires a second parameter (ResultSetMapping) to be able to map the resultsets to Objects.

But when updating (or inserting, or set, or...) there is no resulset to map. Passing null or just new ResultSetMapping(), gives an error.

Are only select queries supported for native sql?


Essentially ditto w/ faken,

this from the docs:

If you want to execute DELETE, UPDATE or INSERT statements the Native SQL API cannot be used and will probably throw errors. Use EntityManager#getConnection() to access the native database connection and call the executeUpdate() method for these queries.

one note of use is the connection object can be retrieved from the EntityManager:

$conn = $entityManager->getConnection();
$rowsAffected = $conn->executeUpdate($sql, $params, $types);


Update statements are usually pretty simple, so you might as well use the normal Doctrine2 way (i.e. programmatically updating entities and calling EntityManager::flush() OR using DQL Updates).

Having said this, if you really want to use normal SQL, you could always do it like this:

  1. Keep the db connection object you get when creating a connection with Doctrine2:

    $connection = \Doctrine\DBAL\DriverManager::getConnection($dbConfig->toArray(),null,$evm);

  2. Execute whatever SQL you want, using the available methods in the connection object, e.g.:

    $connection->executeUpdate($sql, $params, $types); $connection->exec($sql); ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜