Unable to delete a record through Zend_Db_Table_Abstract->delete()

房东的猫 提交于 2019-12-08 09:36:54

问题


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

回答1:


this call

$a = $this->getDbTable()->delete('id ='.(int) $id);

should be like this

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



回答2:


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()



来源:https://stackoverflow.com/questions/20477145/unable-to-delete-a-record-through-zend-db-table-abstract-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!