使用 JOIN 从 Zend_Db_Table 中删除一行
我需要使用引用 rence 表的 Zend_Db_Table 删除一条记录。 在 SQL 中,查询将如下所示:
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
有没有比下面的代码更优雅的方法?
$table = new Application_Model_DbTable_T1();
$sql = 'DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;';
$table->getAdapter()->query($sql);
我找到了一个类似主题,看起来我应该使用$table->getAdapter()->query($sql);
但我希望更好。
I need to delete a record using Zend_Db_Table referencing the rence table.
In SQL the query would look like this:
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
Is there a way to do this more elegant than the code below?
$table = new Application_Model_DbTable_T1();
$sql = 'DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;';
$table->getAdapter()->query($sql);
I found a similar topic and it looks like I should use $table->getAdapter()->query($sql);
but I hope for better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你描述的方式就是正确的做法。
我假设
Application_Model_DbTable_T1
扩展Zend_Db_Table_Abstract
或其子类。Zend_Db_Table_Abstract
的特点是它只适用于单个表。并且您正在尝试访问此查询中的多个表。因此,执行此操作的方法与您正在执行的方法相同。检索Adapter,它不依赖于表,因此可以同时与多个表交互。
TL;DR:这是正确的方法。
No, the way you discribe it is the right way to do it.
I assume that
Application_Model_DbTable_T1
extendsZend_Db_Table_Abstract
or its subclass. The thing aboutZend_Db_Table_Abstract
is that it is meant to only work with a single table. And you are trying to access more than one table in this query.So the way to do this is the same way you are doing. Retrieve the Adapter, which does not depend on the table, and thus can interact with more than one table at the time.
TL;DR: This is the right way.