Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 791
Minras Avatar asked Nov 13 '22 13:11

Minras


1 Answers

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.

like image 147
Janis Peisenieks Avatar answered Nov 16 '22 03:11

Janis Peisenieks