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,
       ));
}
1
  • You could use InnoDB to use referential integrity, and get cascading deletes out of the box... Commented Aug 16, 2011 at 15:44

1 Answer 1

1
+50

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

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry - I should have said I'm using MySQL - which as noted in that section of the docs doesn't support referencial integrity.
@Chris Anstey: MySQL's InnoDB engine supports foreign keys and cascading actions.
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.
@Chris Anstey: to dinopmi for sure :)
@chelmertz: Thanks :) Although maybe it should be for both of us, to be fair.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.