2

I'm developing an application in Zend Framework to handle the rentals for a commercial property rental company. The company has multiple buildings which each have multiple floors, which each have multiple units.

The models I've setup just extend Zend_Db_Table_Abstract, and I've set them up with $_dependentTables and $_referenceMaps with cascading delete, such that when I delete a floor, all the units within it are deleted too, and when I delete a building, all the floors in it are deleted. However, when I delete a building and the floors are deleted, the delete is not cascaded through to each floor's units. (edit: I'm using MySQL, so I am not able to use referencial integrity at the db level.)

I've looked at how the deletes are cascaded, and it appears they aren't cascading because the cacaded deletes are executed using a Zend_Db_Table object, not a Zend_Db_Table_Row object (which you have to use to achieve cascading).

Is there any way I can update the system so that the delete cascades all the way down? Is there a way I can modify the relationships of my classes, or would I need to use something like Doctrine?

(I guess I could override the delete() method for the row of each table or something, but I just wondered if this is possible using the relationships functionality of ZF?)

If it helps, here's the relevant parts of the class definitions:

class Buildings extends Zend_Db_Table
{
   protected $_dependentTables = array('Floors');
}

class Floors extends Zend_Db_Table
{
   protected $_dependentTables = array('Units');

   protected $_referenceMap    = array(
        'Building' => array(
            'columns'           => 'building_id',
            'refTableClass'     => 'Buildings',
            'refColumns'        => 'id',
            'onDelete'          => self::CASCADE,
      ));
}

class Units extends Zend_Db_Table
{
    protected $_referenceMap    = array(
        'Floor' => array(
            'columns'           => 'floor_id',
            'refTableClass'     => 'Floors',
            'refColumns'        => 'id',
            'onDelete'          => self::CASCADE,
       ));
}
ChrisA
  • 2,091
  • 1
  • 14
  • 23

1 Answers1

1

Just to be sure... Are you using a RDBMS that doesn't support referencial integrity?

For my taste, it's easier (and more portable, in case you decide to access the DB from another application in the future) to declare the ON DELETE CASCADE in your RDBMS (provided that it allows it), instead of emulating it with the framework.

It seems that the Zend Framework documentation also advices in this sense: http://framework.zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.cascading

dinopmi
  • 2,683
  • 19
  • 24
  • Sorry - I should have said I'm using MySQL - which as noted in that section of the docs doesn't support referencial integrity. – ChrisA Aug 16 '11 at 10:16
  • 2
    @Chris Anstey: MySQL's InnoDB engine supports foreign keys and cascading actions. – chelmertz Aug 16 '11 at 15:26
  • Thats working great now - all in MySQL. Thanks @chelmertz. Do you want to write an answer so I can assign bounty to you? Or shall I just give it to dinopmi since he was mostly there. – ChrisA Aug 17 '11 at 08:46
  • @Chris Anstey: to dinopmi for sure :) – chelmertz Aug 17 '11 at 13:11
  • @chelmertz: Thanks :) Although maybe it should be for both of us, to be fair. – dinopmi Aug 17 '11 at 13:40