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

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));
dev1234
  • 5,376
  • 15
  • 56
  • 115

2 Answers2

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 );
ins0
  • 3,918
  • 1
  • 20
  • 28
  • It does delete all the records in the table. i only need to delete the record belongs to given ID. – dev1234 Dec 09 '13 at 18:16
  • According to the doc, it says both way is possible. either array or string. http://framework.zend.com/apidoc/1.6/Zend_Db/Table/Zend_Db_Table_Abstract.html#delete – dev1234 Dec 09 '13 at 18:21
  • 1
    ok let me check this on my system...i can't see the error, too – ins0 Dec 09 '13 at 18:27
  • ok. in the meantime, check this answer as well to get some basic idea. http://stackoverflow.com/questions/1845153/zend-framework-how-to-delete-a-table-row-where-multiple-things-are-true please update the answer when the correct way is figured out. – dev1234 Dec 09 '13 at 18:30
  • nope, now it doesnt delete anything. i updated as your change. $this->getDbTable()->delete(array('id = ?' => (int) $id)); always goes to else condition and prints 'Post not deleted' – dev1234 Dec 09 '13 at 18:35
  • damn, last time using zf1 was a couple of months ago - check edit – ins0 Dec 09 '13 at 18:40
  • unfortunately, for the latest change also it didint work. not deleting. it fails and goes to else part. this must be a small thing for a zend dev. i am a newcomer to zend recently. i am updating the question with all the attempts made to resolve this. – dev1234 Dec 09 '13 at 18:43
0

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

dev1234
  • 5,376
  • 15
  • 56
  • 115