使用Data Mapper模型帮助在Zend Framework 1.8.3中定义参照完整性

时间:2009-07-21 20:34:11

标签: zend-framework referential-integrity

我无法使用manual中记录的Zend Frameworks表关系定义参照完整性关系。

我已经用尽了所有可能的配置(看起来似乎如此),现在面临着开发应用程序而无法使用级联删除的前景,这是不可取的。

任何人都可以看到错误或提供任何建议吗?

我的项目使用新的ZF 1.8.3推荐方法和数据处理器进行设置,我想知道这是否是我无法复制参考指南中所述行为的原因。有没有人有这个问题的经验?

以下是课程的相关部分:

应用/模型/ UsersMapper.php

class Default_Model_UsersMapper {
    public function deleteUser($id, Default_Model_Users $users){

    $usersTable = new Default_Model_DbTable_Users();        
    $usersRowset = $usersTable->find( $id );    
    $userToDelete = $usersRowset->current();    
    $userToDelete->delete();

}

应用/模型/ DBTABLE / Users.php

class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
/**
 * @var string Name of the database table
 */
protected $_name = 'users';

/**
 * @desc  reference map 
 * 
 */
 protected $_referenceMap = array(
    'Comment' => array(
    'columns'       =>  array('user_id'),
        'refTableClass' =>  'Comment',
        'refColumns'    =>  array('id'),
        'onDelete'      =>  self::CASCADE
    )
);

}

应用/模型/ CommentMapper.php

...以及该类定义的相关表:

Comment.php

class Default_Model_DbTable_Comment extends Zend_Db_Table_Abstract
{
/**
 * @var string Name of the database table
 */
protected $_name = 'comment';

    /**
 * @desc Defining referential integrity here since we are using MyISAM
 * Dependent tables are referred via the class name. 
 */
protected $_dependentTables = array('Users');
}

2 个答案:

答案 0 :(得分:1)

感谢您发现类定义中的错误。

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



}

答案 1 :(得分:0)

哦,我现在看到了。您的类名为Default_Model_DbTable_Xxx,但您在类中将它们称为Xxx。根据建议进行更改并将您的类名更改为有效:

e.g。

protected $_dependentTables = array('Comment');

'refTableClass' =>      'User',

变为

protected $_dependentTables = array('Default_Model_DbTable_Comment');

'refTableClass' => 'Default_Model_DbTable_User'