目前,我按以下方式初始化数据库:
$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
)
);
});
但是当mysql凭据错误或数据库不存在时,Phalcon返回空白页面而没有任何错误消息。这种情况真的很头疼,因为登录/密码/主机中的小错误导致几乎无法检测到的错误。
我认为Pdo构造函数在这种情况下会停止PHP代码执行。
那么当您希望网站告诉您数据库问题时,如何在Phalcon中初始化数据库连接?
答案 0 :(得分:3)
我结束了以下代码:
$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;
});
请告诉我是否有更好的方法。
答案 1 :(得分:2)
我使用此配置:
$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);
对于utf8还有一个选项,如果应用程序环境是开发,它会将所有sql调用记录到sql_debug.log。您可以看到,我使用的是$ config,其定义如下:
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;
}
在配置文件中你可以找到这样的东西:
[database]
adapter = Mysql
host = localhost
username = root
password =
dbname = db_name
答案 2 :(得分:1)
你可以使它更通用。你可以捕获可能发生的所有错误并很好地显示它们。 我使用下面的bootstrap文件(config)进行开发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();
结论是有趣的部分在这里:
$debug = new Phalcon\Debug();
$debug->listen();
答案 3 :(得分:0)
在phalcon.ini中设置错误报告有帮助吗?
extension=phalcon.so
error_reporting = E_ALL | E_STRICT
display_errors = On