Zend CASCADE 多对多关系

发布于 2025-01-07 05:05:28 字数 1409 浏览 0 评论 0原文

我使用默认的 Zend_Db_Table_Abstract 来设置关系。这些是我的模型:

Model_Table_Project:

protected $_name = 'projects';
protected $_dependentTables = array('Model_Table_ProjectStage');
protected $_referenceMap = array(
    'User' => array(
        'columns' => 'userId',
        'refTableClass' => 'Model_Table_User',
        'refColumns' => 'id'
    )
);

public function deleteProject($id)
{
    $row = $this->find($id)->current();
    if ($row)
    {
        $row->delete();
    }
    else
    {
        throw new Zend_Exception('Cannot delete project.');
    }
}

Model_Table_Stage:

protected $_name = 'stages';
protected $_dependentTables = array('Model_Table_ProjectStage');

Model_Table_ProjectStage:

protected $_name = 'projectstage';
protected $_referenceMap = array(
    'Project' => array(
        'columns' => 'projectId',
        'refTableClass' => 'Model_Table_Project',
        'refColumns' => 'id',
        'onDelete' => self::CASCADE,
    ),
    'Stage' => array(
        'columns' => 'stageId',
        'refTableClass' => 'Model_Table_Stage',
        'refColumns' => 'id',
        'onDelete' => self::CASCADE,
    )
);

现在,当我想删除项目时,使用 Model_Table_Project 中的 deleteProject() 方法,它会删除 Project 表中的条目和 ProjectStage 表中的条目,但它不会删除阶段表中的条目。

我做错了什么?

I'm using the default Zend_Db_Table_Abstract to set the relationships. These are my models:

Model_Table_Project:

protected $_name = 'projects';
protected $_dependentTables = array('Model_Table_ProjectStage');
protected $_referenceMap = array(
    'User' => array(
        'columns' => 'userId',
        'refTableClass' => 'Model_Table_User',
        'refColumns' => 'id'
    )
);

public function deleteProject($id)
{
    $row = $this->find($id)->current();
    if ($row)
    {
        $row->delete();
    }
    else
    {
        throw new Zend_Exception('Cannot delete project.');
    }
}

Model_Table_Stage:

protected $_name = 'stages';
protected $_dependentTables = array('Model_Table_ProjectStage');

Model_Table_ProjectStage:

protected $_name = 'projectstage';
protected $_referenceMap = array(
    'Project' => array(
        'columns' => 'projectId',
        'refTableClass' => 'Model_Table_Project',
        'refColumns' => 'id',
        'onDelete' => self::CASCADE,
    ),
    'Stage' => array(
        'columns' => 'stageId',
        'refTableClass' => 'Model_Table_Stage',
        'refColumns' => 'id',
        'onDelete' => self::CASCADE,
    )
);

Now when I want to delete a project, using the deleteProject() method in Model_Table_Project, it deletes the entry in the Project table and the entries in the ProjectStage table, but it doesn't delete the entries in the Stage table.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小帐篷 2025-01-14 05:05:28

我找到了解决方案。这是我自己的一个思维错误。我现在使用的解决方案是:

在表模型中我调用 $row->delete() ,在行模型中我声明一个删除方法:

public function delete()
{
    $stages = $this->getStages();

    foreach ($stages as $stage)
    {
        $stage->delete();
    }

    return parent::delete();
}

这样它首先删除所有相关阶段,然后删除自身。

I've found the solution. It was a thinking error of myself. The solution I now use is:

In the Table model I call $row->delete() and in the Row model I declared a delete method:

public function delete()
{
    $stages = $this->getStages();

    foreach ($stages as $stage)
    {
        $stage->delete();
    }

    return parent::delete();
}

This way it first delete all relating stages and then it deletes itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文