Zend CASCADE 多对多关系
我使用默认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案。这是我自己的一个思维错误。我现在使用的解决方案是:
在表模型中我调用 $row->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:
This way it first delete all relating stages and then it deletes itself.