How to capture the result of a doctrine2 execution?

0

I am implementing doctrine in a project, however I have a question about inserting, updating and removing data. When I execute 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 see if it actually gave the persist and also successfully executed the flush? In case I could not get his return yet. I tried with try catch, but Exception always returns empty.

I wonder if there is anything like mysqli's affected_rows to use in doctrine

    
asked by anonymous 16.12.2014 / 12:51

3 answers

1

The EntityManager class of Doctrine, which uses the classes that implement the Connection interface (that is, the PDOConnection , DB2Connection , MysqliConnection , OCI8Connection , and SQLSrvConnection classes) make the connection to the databases, does not take advantage of counting rows modified (or returned) by a query.

What you can do is try to catch the errors through the block try / catch or check directly on the connection if there were any errors using the methods:

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

or

$entityManager->getConnection()->errorInfo()
    
30.12.2014 / 14:28
0

You can link Doctrine's SQL log into the application and check in the database if the logs 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 main config.yml of the application, put the doctrine: dbal: logging: key to true and all sqls generated by Doctrine will be logged in the main application log.

    
16.12.2014 / 16:02
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 data captured from the database, including the id. You can make it work.

    
25.08.2017 / 19:44