Pregunta

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));
¿Fue útil?

Solución 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()

Otros consejos

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 );
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top