无法通过 Zend_Db_Table_Abstract->delete() 删除记录 [英] Unable to delete a record through Zend_Db_Table_Abstract->delete()

查看:38
本文介绍了无法通过 Zend_Db_Table_Abstract->delete() 删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Zend 框架模型通过删除操作来删除记录.我仍然无法弄清楚为什么它没有删除并且 $model->delete() 总是返回零.(0) 这是我的删除操作代码.

I am trying to delete a record through a delete action using zend framework Model. i am still unable to figure out why its not deleting and the $model->delete() always returns zero. (0) this is my delete action code.

public function deleteAction()
{
    if ($this->getRequest()->isGet()) {
        $id = $this->getRequest()->getParam('id');
        if (!empty($id)) {
            $id = $this->getRequest()->getPost('id');
            $post = new Application_Model_Post();
            if ($post->delete($id)) {
                $this->_helper->flashMessenger->addMessage('Post deleted.');
            } else {
                $this->_helper->flashMessenger->addMessage('Post note deleted.');
            }
            $this->view->messages = $this->_helper->flashMessenger->getMessages();
        }
        $this->_helper->redirector('index');
    }
}

Application_Model_Post 类有一个方法叫做 delete()

The class Application_Model_Post has a method called, delete()

public function delete($id)
    {
        return $this->getMapper()->delete($id);
    }

指的是Application_Model_PostMapper类中的delete方法

it refers to the delete method in, class Application_Model_PostMapper

  public function delete($id)
    {
        $this->getDbTable()->delete('id ='.(int) $id);

    }
public function getDbTable()
{
    if (null === $this->_dbTable) {
        $this->setDbTable('Application_Model_DbTable_Post');
    }

    return $this->_dbTable;
}
class Application_Model_DbTable_Post extends Zend_Db_Table_Abstract
{
    protected $_name = 'posts';
}

参考:http://framework.zend.com/apidoc/1.6/Zend_Db/Table/Zend_Db_Table_Abstract.html#delete

更新 1

尝试了此解决方案,但无法解决.

tried this solution and unable to get it resolved.

$where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', (int)$id);
$this->getDbTable()->delete( $where );

还有这个,

$this->getDbTable()->delete(array('id = ?' => (int) $id));

推荐答案

问题是,$id 输入错误.

$id = $this->getRequest()->getParam('id');
            if (!empty($id)) {
                $id = $this->getRequest()->getPost('id');
      }

因为我发送了一个获取请求,所以失败了.$id = $this->getRequest()->getPost('id')

Since this i am sending a get request, this fails. $id = $this->getRequest()->getPost('id')

getPost() 应该是 getParam()

这篇关于无法通过 Zend_Db_Table_Abstract->delete() 删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆