How to properly initialize a database connection in Phalcon

at the moment, I initialize the database as follows:

$di->set('db', function() use ($config){ return new \Phalcon\Db\Adapter\Pdo\Mysql( array( "host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name ) ); }); 

but when the mysql credentials are incorrect or the database does not exist, Phalcon returns a blank page without an error message. Such situations are a real headache, since a small error in the login / password / host causes an almost unrivaled error.

I think the Pdo constructor stops the execution of the PHP code in this case.

So, how do you initialize a database connection in Phalcon when you want a website to tell you about a database problem?

+5
source share
4 answers

I finished the following code:

 $di->set('db', function() use ($config){ try { $db = new \Phalcon\Db\Adapter\Pdo\Mysql( array( "host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name ) ); } catch (Exception $e) { die("<b>Error when initializing database connection:</b> " . $e->getMessage()); } return $db; }); 

Please tell me the best way.

+3
source

I am using this configuration:

 $di->set('db', function () use ($config) { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" ) )); if (APPLICATION_ENV == 'development') { $eventsManager = new Phalcon\Events\Manager(); $logger = new Phalcon\Logger\Adapter\File($config->application->logsDir . "sql_debug.log"); //Listen all the database events $eventsManager->attach('db', function ($event, $connection) use ($logger) { if ($event->getType() == 'beforeQuery') { $logger->log($connection->getSQLStatement(), Logger::INFO); } }); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); } return $connection; }, true); 

There is another parameter for utf8, and if the application environment is development, it logs all sql calls to sql_debug.log. You can see that I am using $ config, it is defined as follows:

 switch (APPLICATION_ENV) { case 'development': $config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config_dev.ini'); break; case 'testing': $config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config_test.ini'); break; case 'production': $config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config_production.ini'); break; } 

and in the configuration file you will find something like this:

 [database] adapter = Mysql host = localhost username = root password = dbname = db_name 
+2
source

You can make it more general. You can catch all the errors that may occur and display them beautifully. I use the below boot file (config) for development as described here .

 <?php error_reporting(-1); $debug = new Phalcon\Debug(); $debug->listen(); /** Read the configuration */ $config = include __DIR__ . '/../config/config.php'; /** Read auto-loader */ include __DIR__ . "/../app/config/loader.php"; /** Read services */ include __DIR__ . "/../app/config/mvc/services.dev.php"; /** Handle the request */ $application = new \Phalcon\Mvc\Application(); $application->setDI($di); if (!empty($_SERVER['REQUEST_URI'])) { $uriParts = explode('?', $_SERVER['REQUEST_URI']); $uri = $uriParts[0]; } else { $uri = '/'; } echo $application->handle($uri)->getContent(); 

Which concludes that the interesting part is here:

 $debug = new Phalcon\Debug(); $debug->listen(); 
+1
source

Does the error message in your help report phalcon.ini?

 extension=phalcon.so error_reporting = E_ALL | E_STRICT display_errors = On 
0
source

Source: https://habr.com/ru/post/1200216/


All Articles