开发者

Multiple level dependency models in Zend Framework

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 c开发者_Python百科acaded 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,
       ));
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜