简体   繁体   中英

Delete a row from Zend_Db_Table using JOIN

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.

No, the way you discribe it is the right way to do it.

I assume that Application_Model_DbTable_T1 extends Zend_Db_Table_Abstract or its subclass. The thing about Zend_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM