Zend Framework - 使用表数据映射器模式进行级联删除

时间:2009-07-22 07:50:51

标签: zend-framework referential-integrity

我的问题是让Zend Framework提供DRI层现在可以这样概括。

使用下面的类定义,我可以通过本地UserController“public / users / delete / userId / 22”删除用户但不能删除相关注释,即使我已经设置了一个refernece map和table relationship definition。

当我删除用户记录时,有没有人对为什么没有删除相关评论记录有任何答案?

    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 :(得分:0)

我已经创建了你的模型,并且在测试时它似乎只有在数组中列出了依赖表时才会起作用:

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



}

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

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

您的环境中可能未显示此错误。