7 php database connection cakephp
我有几个用Cakephp构建的网站.如果这些站点中的任何站点由于某种原因而无法正常处理它们与数据库的连接.基本上它会让自己内部尝试反复显示错误,直到浏览器崩溃.内部渲染本身是由元素使用requestAction引起的.
我想知道的是如何检查数据库连接是否存在
我在过滤之前在app_controller中尝试了这个:
if(!ConnectionManager::getDataSource('default'))
{
die(); //this will be a message instead
}
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.
谢谢
使用以下代码:
<?php
$filePresent = true;
if (!file_exists(CONFIGS.'database.php')):
echo '<span class="notice-failure">Database configuration file is not present. Please contact admin@website</span>';
$filePresent = false;
endif;
if ($filePresent!=false):
uses('model' . DS . 'connection_manager');
$db = ConnectionManager::getInstance();
@$connected = $db->getDataSource('default');
if (!$connected->isConnected()):
echo '<p><span class="notice-failure">Not able to connect to the database. Please contact admin@website</span></p>';
endif;
endif;
?>
Run Code Online (Sandbox Code Playgroud)
在这里我打印消息(在那些标签中).您可以使用die()替换回显线.