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?
Yes. The cascading write options ONLY work via the Row object.
In your method doSth you are using the table's delete() method, which
does not enforce the cascade specified in the $_referenceMap array.
In the example previously you are using the row object's delete().
This does enforce the cascade.
If you change your doSth() method to use fetchRow() and then call
delete on that all should work as you intend.
I hope this helps. I have been caught out by this one myself and
(still) am not sure WHY the table's delete() method doesn't behave as
you expected.
Regards,
Carlton