How to capture the result of an execution of doctrine2?

Asked

Viewed 199 times

0

I am implementing Doctrine in a project, but I have a question about inserting, updating and removing data. When I run the following command for example:

$companyName = $entityManager->getRepository("Admin\\Module\\Configuration\\Entity\\Config")->findOneBy(array("type"=>"company_name")); 
$companyName->setValue($POST["companyName"]); 
$entityManager->persist($companyName);
$entityManager->flush();

How can I capture the Doctrine result to know if it actually gave the persist and also ran the flush successfully? In case I couldn’t catch his return yet. I tried with Try catch, but the Exception always comes back empty.

I wonder if there is something similar to the affected_rows of mysqli to use in Doctrine

3 answers

1

The class EntityManager of Doctrine, which uses the classes that implement the interface Connection (that is, the classes PDOConnection, DB2Connection, MysqliConnection, OCI8Connection and SQLSrvConnection) in order to connect to databases, do not take advantage of the count of modified (or returned) lines by a query.

What you can do is try to capture the mistakes through the block try / catch or directly check the connection if there was an error using the methods:

$entityManager->getConnection()->errorCode()

or

$entityManager->getConnection()->errorInfo()
  • It is worth asking if there is an extension or third-party api that implements this count in Doctrine?

  • If the value of affected lines is not exposed I believe it is for some good reason, or because you can get the value otherwise (for example, the size of your initial resultset). Is there a case you really need that count?

  • is relative in fact, I am making insertions in ajax and wanted to give the answer containing the number of affected lines and also wanted to use to validate the return in some cases. But mostly it is more for didactic reasons, it is always good to know how far the possibilities of a tool extend

0

You can turn on the Doctrine SQL log in the application and check the database to see if the records are there. If the flush has not been made the records will not appear in the bank. If you are using Doctrine with Symfony, in the application’s main config.yml, put the key Doctrine: dbal: logging: for true and all Doctrine generated sqls will be logged in the application’s main log.

  • I am not using symfony in the application and checking the logs does not seem to me the most correct way to do this check or capture. A simple example is when we use mysqli and can capture affected_rows in the query, which I haven’t found in Doctrine yet. and that’s basically what I would need.

0

$companyName = $entityManager->getRepository("Admin\\Module\\Configuration\\Entity\\Config")

->findOneBy(array("type"=>"company_name")); 

$companyName->setValue($POST["companyName"]); 

$entityManager->persist($companyName);

$entityManager->flush();

var_dump($companyName);

It will print an object with the captured data from the database, including the id. You can do it right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.