Zend Framework - 使用表数据映射器模式进行级联删除
我让 Zend Framework 提供 DRI 层的问题现在可以总结如下。
使用下面的类定义,我可以通过本地 UserController“public/users/delete/userId/22”删除用户,但不能删除相关注释,即使我已经设置了引用映射和表关系定义。
有谁知道为什么我删除用户记录时关联的评论记录没有被删除吗?
class Default_Model_DbTable_Comment extends Zend_Db_Table_Abstract
{
/**
* @var string Name of the database table
*/
protected $_name = 'comment';
/**
* @desc reference map
*
* Rows in the comment table are to be automatically deleted if the row in the
* User table to which they refer is deleted
*
*/
protected $_referenceMap = array(
'User' => array(
'columns' => 'user_id', // the foreign key(s)
'refTableClass' => 'Default_Model_DbTable_Users',
'refColumns' => 'id',
'onDelete' => self::CASCADE,
)
);
}
class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
/**
* @var string Name of the database table
*/
protected $_name = 'users';
/**
* @desc Defining referential integrity here since we are using MyISAM
* Dependent tables are referred via the class name.
*/
protected $_dependentTables = 'Default_Model_DbTable_Comment';
}
My problem getting Zend Framework to provide a DRI layer can now be summarized as such.
Using the class definitions below I am able to delete the user but not the related comment through my local UserController "public/users/delete/userId/22", even though I have set up a refernece map and table relationship definition.
Does anyone have any answers to why the associated comment record is not deleted when i delete the users record?
class Default_Model_DbTable_Comment extends Zend_Db_Table_Abstract
{
/**
* @var string Name of the database table
*/
protected $_name = 'comment';
/**
* @desc reference map
*
* Rows in the comment table are to be automatically deleted if the row in the
* User table to which they refer is deleted
*
*/
protected $_referenceMap = array(
'User' => array(
'columns' => 'user_id', // the foreign key(s)
'refTableClass' => 'Default_Model_DbTable_Users',
'refColumns' => 'id',
'onDelete' => self::CASCADE,
)
);
}
class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
/**
* @var string Name of the database table
*/
protected $_name = 'users';
/**
* @desc Defining referential integrity here since we are using MyISAM
* Dependent tables are referred via the class name.
*/
protected $_dependentTables = 'Default_Model_DbTable_Comment';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经创建了您的模型,并且在测试时发现它仅在数组中列出依赖表时才有效:
当它们未在数组中列出时,我收到错误
警告:为 C 中的 foreach() 提供的参数无效:\PHP\includes\ZendFramework-1.8.4-minimal\library\Zend\Db\Table\Row\Abstract.php on line 632
此错误在您的环境中可能不可见。
I've created models as yours and on testing it appears that it only works if dependent tables are listed in an array:
When they aren't listed in an array, I get the error
Warning: Invalid argument supplied for foreach() in C:\PHP\includes\ZendFramework-1.8.4-minimal\library\Zend\Db\Table\Row\Abstract.php on line 632
This error may not have been visible in your environment.