[英]Unable to delete a record through Zend_Db_Table_Abstract->delete()
我正在嘗試使用zend Framework Model通過刪除操作刪除記錄。 我仍然無法弄清楚為什么不刪除它,而$ model-> delete()總是返回零。 (0)這是我的刪除操作代碼。
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()的方法
public function delete($id)
{
return $this->getMapper()->delete($id);
}
它引用類Application_Model_PostMapper中的delete方法
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
嘗試了此解決方案,但無法解決。
$where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', (int)$id);
$this->getDbTable()->delete( $where );
還有這個
$this->getDbTable()->delete(array('id = ?' => (int) $id));
這個電話
$a = $this->getDbTable()->delete('id ='.(int) $id);
應該是這樣的
$where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', (int)$id);
$this->getDbTable()->delete( $where );
問題是,為$ id輸入了錯誤的輸入。
$id = $this->getRequest()->getParam('id');
if (!empty($id)) {
$id = $this->getRequest()->getPost('id');
}
由於這是我發送的獲取請求,因此失敗。 $ id = $ this-> getRequest()-> getPost('id')
getPost()應該是getParam()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.