在丢弃之前检查表是否存在?

Cas*_*sey 8 doctrine doctrine-1.2

我试图在删除之前检查表的存在.我已经阅读了Doctrine_Table的API文档,我似乎找不到这样的东西.有什么我想念的吗?

我有代码看起来像:

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

$export = new Doctrine_Export();

$export->dropTable($table->getTableName());
Run Code Online (Sandbox Code Playgroud)

当表不存在时我得到的错误是:

致命错误:未捕获异常'Doctrine_Connection_Mysql_Exception',消息'SQLSTATE [42S02]:未找到基表或视图:1051未知表

提前致谢,

卡西

ple*_*ock 23

Doctrine2方法是:

$schemaManager = $this->getDoctrine()->getConnection()->getSchemaManager();
if ($schemaManager->tablesExist(array('users')) == true) {
      // table exists! ...
}
Run Code Online (Sandbox Code Playgroud)


tar*_*ion 5

如果您只想在表存在的情况下返回true / false,这就是我所做的:

public function checkTable($table)
{
    $conn = Doctrine_Manager::connection();
    try { $conn->execute("DESC $table"); }
    catch (Exception $e) { return false; }
    return true;
}
Run Code Online (Sandbox Code Playgroud)


Cas*_*sey 2

这是我最终使用的...欢迎任何改进建议:

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;
}
Run Code Online (Sandbox Code Playgroud)