0

当我尝试从数据库中删除实体时,我需要捕获并处理错误。

[删除记录时出错:SQLSTATE[23000]:完整性约束违规:1451 无法删除或更新父行:外键约束失败(zdf. cats, CONSTRAINT FK_cats_CategoriesFOREIGN KEY ( category_id) REFERENCES categories( id) ON DELETE NO ACTION ON UPDATE NO ACTION)]

我尝试通过单击“deleteURL”按钮删除实体

  $dbCategories = new Application_Model_DbTable_Categories();
    $source = new Bvb_Grid_Source_Zend_Select($dbCategories->getCategoriesByAppId(1, true));
        $columns = array('title', 'actions');
        $columnsPositions = array('title');
        $extraColumns = array(
            0 => $this->createExtraColumn(array(
                'name' => 'actions',
                'position' => 'right',
                'title' => 'Actions',
                'decorator' => '<a class="edit-button" href="{{editUrl}}">&nbsp;</a><a class="remove-button" href="{{deleteUrl}}">&nbsp;</a>'
            ))
    );
4

2 回答 2

1

由于我无法找到如何使用 ZF DataGrid 捕获此错误而无需在组件库中执行某些操作,因此我根据 URL 参数自行删除。

基本上,

$deleteId =  $this->getRequest()->getParams('Delete');

然后Try...Catch$model->delete($id);

并手动处理异常。

于 2012-09-02T07:51:09.607 回答
0

使用 Try/Catch 块怎么样?

try {
    $dbCategories = new Application_Model_DbTable_Categories();
    $source = new Bvb_Grid_Source_Zend_Select($dbCategories->getCategoriesByAppId(1, true));
    $columns = array('title', 'actions');
    $columnsPositions = array('title');
    $extraColumns = array(
        0 => $this->createExtraColumn(array(
            'name' => 'actions',
            'position' => 'right',
            'title' => 'Actions',
            'decorator' => '<a class="edit-button" href="{{editUrl}}">&nbsp;</a><a class="remove-button" href="{{deleteUrl}}">&nbsp;</a>'
        ))
    );
} catch (Exception $e) {
    // handle exception here...
}
于 2012-07-10T14:40:02.483 回答