2010-03-25 3 views
7

Cakephp로 구축 된 사이트가 몇 군데 있습니다. 어떠한 이유로 든이 사이트 중 어떤 것이 데이터베이스에 연결이 끊어지면 제대로 처리하지 못합니다. 기본적으로 브라우저 자체가 오류가 발생할 때까지 반복해서 표시하려고 시도합니다. 자체의 렌더링 자체는 요소의 requestAction 사용으로 인해 발생합니다.Cakephp - 데이터베이스에 연결할 수없고 정상적으로 복구 할 수없는 경우 감지하십시오.

if(!ConnectionManager::getDataSource('default')) 
{ 
    die(); //this will be a message instead 
} 

을하지만, 작동하지 않는 것 : 내가 알고 싶은 무엇

데이터베이스 연결 내가 필터 전에 app_controller이 시도

존재하면 내가 확인 할 수있는 방법입니다.

감사

답변

7

다음 코드를 사용 : 나는 (그 태그) 메시지를 인쇄하고있어 여기에

<?php 
$filePresent = true; 
if (!file_exists(CONFIGS.'database.php')): 
    echo '<span class="notice-failure">Database configuration file is not present. Please contact [email protected]</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 [email protected]</span></p>'; 
    endif; 
endif; 
?> 

합니다. 에코 라인을 die()로 바꿀 수 있습니다.

+0

내가 나에게하는 경우 이메일을 보내려면이를 사용하여 매우 –

+1

감사합니다 DB는 이제까지 아래로 간다! (임 아마존 RDS와 함께), 잘 작동합니다! +1 – Anil

1

(CakePHP의 3.x를) 그냥 PagesController의 홈보기에서 주어진 예에 따라 : 기본적으로

그것입니다

use Cake\Datasource\ConnectionManager; 

    try { 
     $connection = ConnectionManager::get('yourconnection'); 
     $connected = $connection->connect(); 
    } catch (Exception $connectionError) { 
     //Couldn't connect 
    } 
    //connected 
+0

이것은 케이크 3의 새로운 버전에서 작동합니다. 그러나 예외를 잡을 수는 없지만 cake는보기 "src \ Template \ Error \ missing_connection.ctp"를 사용하여 오류 메시지를 봅니다 ... – Snorvarg

+0

"예외"대신 "\ 예외". 다음을 참조하십시오 : https://stackoverflow.com/questions/30995594/how-do-i-throw-a-custom-try-catch-exception-on-cakephp – Snorvarg

관련 문제