How to verify that the connection works in doctrine 2?

I am looking for a way to check if the connection with doctrine 2 works.

Because in my application, users can independently change information connections. I want to check if the user has entered the correct login and correct password.

How can i do this?

I tried putting this code in a try / catch block:

try{ $entityManager = $this->getEntityManager() ; $repository = $entityManager->getRepository('Authentification\Entity\User'); $userToIdentify = $repository->findOneBy(array('login' => $this->_username, 'password' => $this->_password)); }catch(Exception $e){ $code = Result::FAILURE ; $identity = "unknow" ; $messages = array( "message" => "Mauvaise combinaison de login/password", ) ; } 

The problem is that even if the information connections are correct, I cannot catch the exception.

Otherwise, I have this error:

 <b>Fatal error</b>: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template &quot;layout/layout&quot;; resolver could not resolve to a file' in C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php:451 Stack trace: #0 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\View.php(203): Zend\View\Renderer\PhpRenderer-&gt;render(Object(Zend\View\Model\ViewModel)) #1 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\Mvc\View\Http\DefaultRenderingStrategy.php(128): Zend\View\View-&gt;render(Object(Zend\View\Model\ViewModel)) #2 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy-&gt;render(Object(Zend\Mvc\MvcEvent))#3 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(469): call_user_func(Array, Object(Zend\Mvc\MvcEvent))#4 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\EventManager\EventMa in <b>C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php</b> on line <b>451</b><br /> 

Do you have any ideas on how I can check if the connection works?

Thanks.

+6
source share
2 answers

Do not use EntityManager directly. Instead, you can use the following connection verification options:

 try { $entityManager->getConnection()->connect(); } catch (\Exception $e) { // failed to connect } 

This, unfortunately, is the only real way to verify that something went wrong, because the type of exception varies depending on the driver you are using.

For another exception (associated with the view), you just need to configure the path to the view script. I suggest that you support the skeleton application module so that the default layout always exists: you can override it at any time .

+15
source

You can use.

  $cnx = $this->getDoctrine()->getConnection(); $cnx->isConnected() ? 'Connected' : 'not connected'; 
+2
source

All Articles