0

我正在尝试从我的表中删除一个帖子,但代码正在删除所有表数据,而不仅仅是一个帖子。

删除.php

<?php
class Form_Delete extends Zend_Form
{
    public function __construct()
    {
        @parent::__construct($options);
        $this->setName('Delete');
        $id = new Zend_Form_Element_Hidden('id');
        $title = new Zend_Form_Element_Hidden('Title');
        $description = new Zend_Form_Element_Hidden('Description');
        $submit = new Zend_Form_Element_Submit('Delete');
        $submit->setAttrib('id', 'submitbutton');
        $cancel = new Zend_Form_Element_Submit('cancel');
        $cancel->setAttrib('id', 'cancelbutton');
        $this->addElements( array( $id, $title, $description, $submit, $cancel ));
    }
}

和我的控制器代码

public function deleteAction()
    {
        //action body
        $request = $this->getRequest();
        $postid = (int)$request->getParam('id');
        $post = new Model_DbTable_Posts();
        $result = $post->getPost($postid);
        $this->view->post = $result;
        if(!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect('posts/view/id/'.$postid);
        }

        $identity = Zend_Auth::getInstance()->getIdentity();

        $acl = new Model_Acl();
        if( $acl->isAllowed( $identity['Role'] ,'posts','edit','delete') ) {
            $deleteForm = new Form_Delete();
            $deleteModel = new Model_DbTable_Posts();
            if ($this->getRequest()->isPost()) {
                if ($deleteForm->isValid($request->getPost())) {
                    $deleteModel->delete($dpostid);                 
                    $this->_redirect('index/index');
                }
            }
            $this->view->deleteForm = $deleteForm;
        }

我已经尝试了几个小时来让它工作,但可以让它全部删除或全部删除。任何帮助都会很棒!

4

1 回答 1

4

您需要通过delete()where 子句进行评估。尝试:

$where = $deleteModel->getAdapter()->quoteInto('id = ?', $dpostid);
$deleteModel->delete($where);

将“id”更改为您的主键。

@parent::__construct($options);您的表单类中也有点奇怪。将其更改为parent::__construct();. 如果您不生成错误,则无需抑制错误。

于 2012-08-07T19:16:57.070 回答