2010-07-10 8 views
7

테이블을 삭제하기 전에 테이블의 존재를 확인하려고합니다. Doctrine_Table에 대한 API 문서를 읽었으며 이와 같은 것을 찾을 수 없습니다. 내가 빠진 것이 있습니까?삭제하기 전에 테이블 존재 여부를 확인 하시겠습니까?

나는 보이는 코드를 가지고 같은 :

$table = new Doctrine_Table('model_name', $conn); 

$export = new Doctrine_Export(); 

$export->dropTable($table->getTableName()); 

그리고 테이블이 존재하지 않을 때 내가 오류는 다음과 같습니다

치명적인 오류 : catch되지 않은 예외 'Doctrine_Connection_Mysql_Exception'메시지 ' SQLSTATE [42S02] : 기본 테이블이나 발견되지 뷰 : 1051 알 수없는 테이블 사전에

감사합니다,

public static function isInstalled() 
{ 
    $installed = true; 

    $q = Doctrine_Query::create($conn); 
    $q->select('t.id'); 
    $q->from('Table t'); //the table to check 

    try { 
     $q->execute(); 
    } catch (Doctrine_Connection_Exception $e) { 
     // we only want to silence 'no such table' errors 
     if ($e->getPortableCode() !== Doctrine_Core::ERR_NOSUCHTABLE) { 
      throw new Doctrine_Export_Exception($e->getMessage()); 
     } 

     $installed = false; 
    } 

    return $installed; 
} 

답변

2

여기 내가 사용하여 상처 무슨 ... 개선을위한 제안을 환영합니다

DROP TABLE IF EXISTS ... 

Doctrine으로 원시 SQL 쿼리를 실행할 수도 있습니다.

public function checkTable($table) 
{ 
    $conn = Doctrine_Manager::connection(); 
    try { $conn->execute("DESC $table"); } 
    catch (Exception $e) { return false; } 
    return true; 
} 
+1

(그러나 이것은 관련 – lotsoffreetime

0

나는 휴대 성을 테스트하지 않은,하지만 네이티브 SQL에 당신이 할 수 있습니다 :케이시

4

그냥 테이블이 존재하는 경우 true/false를 반환 할 경우

이 내가 무슨 짓을) 먼저 dropTable()에 의해 throw 된 Doctrine_Connection_Mysql_Exception (또는 그 불가지론 자의 부모)을 잡아서 오류가 나타나면 무시합니다. 두 번째로, Doctrine_Query를 무료로 생성하지 않으면 메모리 누수가 발생합니다.
+0

좋은 제안과 짧은 제안, 내 표결이 하나 있습니다. –

17

Doctrine2 방법은 다음과 같습니다 다음 강령술 죄송

$schemaManager = $this->getDoctrine()->getConnection()->getSchemaManager(); 
if ($schemaManager->tablesExist(array('users')) == true) { 
     // table exists! ... 
} 
+1

이 방법은 매우 직관적입니다. – manix

+0

이것은 가장 간단한 방법이며이를 점검하는 방법입니다. 제 경우에는 Doctrine EntityManager를 통해 SchemaManager에 대한 참조를 얻었습니다. $ em-> getConnection() -> getSchemaManager(); – spetz83

관련 문제