On Tue, Jun 30, 2009 at 12:18 AM, Thomas D.<[email protected]> wrote:
> Hi,
>
> I have two tables, drivers and cars.
> Each driver in drivers has his cars in cars.
>
> When I delete a driver, his cars should also be deleted in cars.
>
> I defined two classes:
>
> class Drivers extends Zend_Db_Table_Abstract
> {
> [...]
> protected $_dependentTables = array(
> 'Cars'
> );
> }
>
> class Cars extends Zend_Db_Table_Abstract
> {
> [...]
> protected $_referenceMap = array(
> 'Pages' => array(
> 'columns' => array('driver_id'),
> 'refTableClass' => 'Drivers',
> 'refColumns' => 'id',
> 'onDelete' => self::CASCADE
> )
> );
> }
>
> When I run
>
> $myTable = new Drivers();
> $driversRowset = $myTable ->find(1278);
> $driver = $driversRowset->current();
>
> $driver->delete();
>
> The row with id 1278 in my drivers table will be deleted and also all rows
> in cars, where driver_id = 1278. Everything is working as expected.
>
> Now I want to add a method to my drivers Zend_Db_Table class, which will
> delete a driver and do something else:
>
> class Drivers extends Zend_Db_Table_Abstract
> {
> [...]
> public function doSth($id)
> {
> $where = $this->_db->quoteInto('id = ?', $id, Zend_Db::INT_TYPE);
> $this->delete($where);
>
> // do my other stuff
> [...]
> }
> }
>
> My problem is, that when I call $driverTable->doSth(1278), the row in the
> driver's table will be deleted, but it won't delete any rows in cars.
>
> Did I miss something?
>
>
> --
> Regards,
> Thomas
>
>
>
You are changing the syntax between the two examples. In the first,
you are calling finding a specific row and
Zend_Db_Table_Row_Abstract::delete() on that row, which is what is
shown in the documentation. This function scans the dependent tables
and deletes all dependent rows.
In the second, you are calling the Zend_Db_Table_Abstract::delete()
method, which simply calls delete() on the database adapter.
It looks like you just need to change your doSth() method to this:
class Drivers extends Zend_Db_Table_Abstract
{
[...]
public function doSth($id)
{
$driversRowset = $this->find($id);
$driver = $driversRowset->current();
$driver->delete();
// do my other stuff
[...]
}
}
Andrew