【问题标题】:Zend Framework - Cascading Deleting Using Table Data Mapper patternZend 框架 - 使用表数据映射器模式进行级联删除
【发布时间】:2009-07-22 07:50:51
【问题描述】:

让 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';



}

【问题讨论】:

    标签: zend-framework referential-integrity


    【解决方案1】:

    我已经创建了和你一样的模型,在测试中它似乎只有在依赖表列在数组中时才有效:

    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 = array('Default_Model_DbTable_Comment');
    
    
    
    }
    

    当它们没有在数组中列出时,我得到错误

    警告:第 632 行 C:\PHP\includes\ZendFramework-1.8.4-minimal\library\Zend\Db\Table\Row\Abstract.php 中的 foreach() 参数无效

    您的环境中可能看不到此错误。

    【讨论】:

    • 你是怎么得到这样的错误的......因为我不觉得 Zend 堆栈跟踪错误很有用?
    • 另外,我刚刚尝试了在数组中定义dependentTables 的建议,应用程序因 Zend 堆栈错误而崩溃:堆栈跟踪:#0 C:\wamp\www\megashare\application\models\ UsersMapper.php(104): Zend_Db_Table_Row_Abstract->delete() #1 C:\wamp\www\megashare\application\models\Users.php(761): Default_Model_UsersMapper->deleteUser(23, Object(Default_Model_Users)) #2 C :\wamp\www\megashare\application\controllers\UsersController.php(113): Default_Model_Users->deleteUser(23)
    • 我认为除了 E_ALL 之外,错误还需要 error_reporting 显示 E_STRICT - 在任何情况下,请确保显示所有错误条件。我的代码和你的唯一区别是我没有为模型类名称使用命名空间,即我的类是用户和评论。我没有使用数据映射器,但这似乎无关紧要;它只是像我一样连续调用 delete()。
    • 我认为数据映射器可能会导致问题,我没有尝试过,但肯定他们必须是使用数据映射器完成这项工作的一种方法?.. 老实说,我正在失去信心目前与 Zend 合作,支持在 Drupal 上尝试我的手。在接下来的 3 天里,我将用 Drupal 进行黑客攻击,看看是否是时候做出改变了……
    猜你喜欢
    • 2012-04-27
    • 2011-11-29
    • 2011-10-05
    • 2011-03-22
    • 2011-06-14
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多