doxzrb8721 2013-12-09 17:46
浏览 52
已采纳

无法通过Zend_Db_Table_Abstract-> delete()删除记录

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');
    }
}

The class Application_Model_Post has a method called, delete()

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

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';
}

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

UPDATE 1

tried this solution and unable to get it resolved.

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

also this,

$this->getDbTable()->delete(array('id = ?' => (int) $id));
  • 写回答

2条回答 默认 最新

  • doujiang3997 2013-12-10 04:34
    关注

    Issue was, taking the wrong input for $id.

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

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

    getPost() should be getParam()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile