Zend Framework 中的多级依赖模型
我正在 Zend Framework 中开发一个应用程序来处理商业地产租赁公司的租金。该公司拥有多栋大楼,每栋大楼都有多层,每栋大楼都有多个单元。
我设置的模型只是扩展了 Zend_Db_Table_Abstract
,并且我使用 $_dependentTables
和 $_referenceMap
进行了级联删除,例如当我删除一个楼层时,其中的所有单元也会被删除,当我删除一座建筑物时,其中的所有楼层都会被删除。但是,当我删除建筑物并删除楼层时,删除不会级联到每个楼层的单元。 (编辑:我正在使用 MySQL,所以我无法在数据库级别使用引用完整性。)
我研究了删除是如何级联的,看起来它们不是级联的,因为级联删除是使用一个 Zend_Db_Table
对象,而不是一个 Zend_Db_Table_Row
对象(您必须使用它来实现级联)。
有什么方法可以更新系统以便删除一直向下级联吗?有没有办法可以修改我的类的关系,或者我需要使用像 Doctrine 这样的东西?
(我想我可以重写每个表的行或其他内容的 delete()
方法,但我只是想知道使用 ZF 的关系功能是否可以实现这一点?)
如果有帮助,这里是相关的部分类定义:
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,
));
}
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 $_referenceMap
s 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,
));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了确定......您使用的 RDBMS 是否不支持引用完整性?
就我的口味而言,在 RDBMS 中声明 ON DELETE CASCADE 更容易(并且更可移植,以防您将来决定从另一个应用程序访问数据库)(前提是它允许),而不是使用框架模拟它。
Zend Framework 文档似乎也从这个意义上提出了建议:
http://framework .zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.cascading
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