在删除之前检查表是否存在?
我试图在删除表之前检查它是否存在。我已经阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Doctrine2方法是:
Doctrine2 method is:
如果您只想在表存在时返回 true/false,这就是我所做的:
If you just want to return true/false if the table exists, this is what I did:
这是我最终使用的...欢迎任何改进建议:
Here's what I wound up using... any suggestions for improvement are welcome:
尝试对表存在检查:
对于列检查:
带跳过的完整示例:
Try this for table exist check:
For columns check:
Full example with skip:
我还没有测试可移植性,但在原生 SQL 中你可以这样做:
你也可以使用 Doctrine 运行原生 SQL 查询。
I haven't tested portability, but in native SQL you can do:
You can run native SQL queries with Doctrine as well.