Can I flush new object (insert) and execute DQL (update some tables) in the same transaction with Doctrine? Or I have to use native SQL query with explicit transaction declaration?
I want to execute something like this in a transaction
INSERT INTO order VALUES (...)
UPDATE user SET email = :email, phone = :phone, status = :status
UPDATE user_counter SET order_counter = order_counter + 1
UPDATE some_counter SET acme_counter = acme_counter + 1
Thank for your help!
Use the wrapInTransaction
function of the entityManager
. You can mix persist/remove with other DQL queries.
Here's an example:
this->entityManager->wrapInTransaction(function ($em) use($phoneNumber, $phoneNumberVerificationRequest){
$this->entityManager->persist($phoneNumber);
$this->entityManager->flush();
$this->phoneNumberVerificationRequestRepository->removeByCountryCallingCodeAndNumber($phoneNumberVerificationRequest->getCountryCallingCode(), $phoneNumberVerificationRequest->getNumber());
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.