在删除之前检查表是否存在?

发布于 2024-09-09 02:56:49 字数 396 浏览 9 评论 0原文

我试图在删除表之前检查它是否存在。我已经阅读了 Doctrine_Table 的 API 文档,但似乎找不到类似的内容。我有什么遗漏的吗?

我的代码如下所示:

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

$export = new Doctrine_Export();

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

当表不存在时出现的错误是:

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

提前致谢,

凯西

I'm trying to check for the existence of a table before dropping it. I've read through the API documentation for Doctrine_Table and I can't seem to find anything like this. Is there something I'm missing?

I've got code that looks like:

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

$export = new Doctrine_Export();

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

And the error I get when a table doesn't exist is:

Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table

Thanks in advance,

Casey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

停滞 2024-09-16 02:56:50

Doctrine2方法是:

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

Doctrine2 method is:

$schemaManager = $this->getDoctrine()->getConnection()->getSchemaManager();
if ($schemaManager->tablesExist(array('users')) == true) {
      // table exists! ...
}
甩你一脸翔 2024-09-16 02:56:50

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

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

If you just want to return true/false if the table exists, this is what I did:

public function checkTable($table)
{
    $conn = Doctrine_Manager::connection();
    try { $conn->execute("DESC $table"); }
    catch (Exception $e) { return false; }
    return true;
}
妳是的陽光 2024-09-16 02:56:50

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

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;
}

Here's what I wound up using... any suggestions for improvement are welcome:

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;
}
小红帽 2024-09-16 02:56:50

尝试对表存在检查:

 public function up(Schema $schema): void
{
    $schema->hasTable('table_name');
}

对于列检查:

$schema->getTable('supply')->hasColumn('contracts')

带跳过的完整示例:

$this->skipIf($schema->getTable('supply')->hasColumn('contracts'), 'Table order_statuses already exist');

Try this for table exist check:

 public function up(Schema $schema): void
{
    $schema->hasTable('table_name');
}

For columns check:

$schema->getTable('supply')->hasColumn('contracts')

Full example with skip:

$this->skipIf($schema->getTable('supply')->hasColumn('contracts'), 'Table order_statuses already exist');
茶底世界 2024-09-16 02:56:50

我还没有测试可移植性,但在原生 SQL 中你可以这样做:

DROP TABLE IF EXISTS ...

你也可以使用 Doctrine 运行原生 SQL 查询。

I haven't tested portability, but in native SQL you can do:

DROP TABLE IF EXISTS ...

You can run native SQL queries with Doctrine as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文