Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how test that the connection works in Doctrine 2?

I'm looking for a way to test if a connection is working or not with Doctrine 2.

As in my application users can change by themselves the information connections, I want to check if the user has entered the right login and password.

How can I do that?

I tried to put this code into 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" => "Wrong login/password combination",
    ) ;
}

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

Otherwise I get the following 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 idea in how I could test if the connection works?

Thank you.

like image 457
Freezer freezer Avatar asked Feb 18 '13 09:02

Freezer freezer


2 Answers

You can use.

$cnx = $this->getDoctrine()->getConnection();

$cnx->isConnected() ? 
  'Connected' : 
  'not connected';
like image 65
mrDjouk Avatar answered Oct 18 '22 19:10

mrDjouk


Do not use the EntityManager directly. You can instead use following to check the connection parameters:

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

That's sadly the only real way to check if something went wrong, since the exception type changes depending on the driver you use.

For the other exception (the view-related one) you simply have to adjust your view scripts path. I suggest you to keep the skeleton application module enabled so that the default layout is always there: you can override it at any time.

like image 24
Ocramius Avatar answered Oct 18 '22 19:10

Ocramius