使用 delete 方法时出现数据库错误异常
Exception with database error while using delete method
我关注http://framework.zend.com/manual/current/en/in-depth-guide/data-binding.html
我有以下代码
PostService.php:
namespace Blog\Service;
use Blog\Mapper\PostMapperInterface;
use Blog\Model\PostInterface;
class PostService implements PostServiceInterface
{
protected $postMapper;
public function __construct(PostMapperInterface $postMapper)
{
$this->postMapper = $postMapper;
}
public function findAllPosts() {
return $this->postMapper->findAll();
}
public function findPost($id) {
return $this->postMapper->find($id);
}
public function savePost(PostInterface $post)
{
return $this->postMapper->save($post);
}
public function deletePost(PostInterface $post)
{
return $this->postMapper->delete($post);
}
}
DeleteController.php
namespace Blog\Controller;
use Blog\Service\PostServiceInterface;
use Zend\Form\FormInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Debug\Debug;
class DeleteController extends AbstractActionController
{
protected $postService;
public function __construct(PostServiceInterface $postService) {
$this->postService = $postService;
}
public function deleteAction()
{
try {
$post = $this->postService->findPost($this->params('id'));
} catch (\InvalidArgumentException $e) {
return $this->redirect()->toRoute('post');
}
$request = $this->getRequest();
var_dump($_SERVER['REQUEST_METHOD']);
if ($request->isPost()) {
$del = $request->getPost('delete_confirmation', 'no');
if ($del === 'yes') {
$this->postService->deletePost($post);
}
return $this->redirect()->toRoute('post');
}
return new ViewModel(array(
'post' => $post
));
}
}
ZendDbSqlMapper.php(保存并删除映射部分)
namespace Blog\Mapper;
use Blog\Model\PostInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\HydratingResultSet; /*used directly below*/
use Zend\Db\Sql\Delete;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Update;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Db\Sql\PreparableSqlInterface;
class ZendDbSqlMapper implements PostMapperInterface{
protected $dbAdapter;
protected $hydrator;
protected $postPrototype;
public function __construct(AdapterInterface $dbAdapter,HydratorInterface $hydrator,PostInterface $postPrototype) {
$this->dbAdapter = $dbAdapter;
$this->hydrator = $hydrator;
$this->postPrototype = $postPrototype;
}
public function save(PostInterface $postObject)
{
$postData = $this->hydrator->extract($postObject);
unset($postData['id']);
if($postObject->getId())
{
$action = new Update('posts');
$action->set($postData);
$action->where(array('id = ?'=>$postObject->getId()));
}
else
{
$action = new Insert('posts');
$action->values($postData);
}
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
if($result instanceof ResultSetInterface)
{
if($newId = $result->getGeneratedValue())
{
$postObject->setId($newId);
}
return $postObject;
}
throw new \Exception('Database error in ZendDbSqlMapper.php');
}
public function delete(PostInterface $postObject)
{
$action = new Delete('posts');
$action->where(array('id = ?' => $postObject->getId()));
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return (bool)$result->getAffectedRows();
}
}
delete.phtml
<h1>DeleteController::deleteAction()</h1>
<p>
Are you sure that you want to delete
'<?php echo $this->escapeHtml($this->post->getTitle()); ?>' by
'<?php echo $this->escapeHtml($this->post->getText()); ?>'?
</p>
<form action="<?php echo $this->url('post/delete', array(), true); ?>" method="post">
<input type="hidden" name="id" value="<?php echo $post->getId(); ?>">
<input type="submit" name="delete_confirmation" value="yes">
<input type="submit" name="delete_confirmation" value="no">
</form>
问题:
当我尝试在 http://www.myzend.com/blog/delete/2
之类的浏览器中删除博客时,它显示
之类的错误
Database error in ZendDbSqlMapper.php
我什么都试过了,但找不到哪里出错了。
我正在使用 Zend framework 2.3.3
如果需要,我可以添加更多详细信息。
我终于找到了 issue.It 在 PostService.php
中,它在 deletePost
中返回 save
方法,这应该是 delete
方法.
我关注http://framework.zend.com/manual/current/en/in-depth-guide/data-binding.html
我有以下代码
PostService.php:
namespace Blog\Service;
use Blog\Mapper\PostMapperInterface;
use Blog\Model\PostInterface;
class PostService implements PostServiceInterface
{
protected $postMapper;
public function __construct(PostMapperInterface $postMapper)
{
$this->postMapper = $postMapper;
}
public function findAllPosts() {
return $this->postMapper->findAll();
}
public function findPost($id) {
return $this->postMapper->find($id);
}
public function savePost(PostInterface $post)
{
return $this->postMapper->save($post);
}
public function deletePost(PostInterface $post)
{
return $this->postMapper->delete($post);
}
}
DeleteController.php
namespace Blog\Controller;
use Blog\Service\PostServiceInterface;
use Zend\Form\FormInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Debug\Debug;
class DeleteController extends AbstractActionController
{
protected $postService;
public function __construct(PostServiceInterface $postService) {
$this->postService = $postService;
}
public function deleteAction()
{
try {
$post = $this->postService->findPost($this->params('id'));
} catch (\InvalidArgumentException $e) {
return $this->redirect()->toRoute('post');
}
$request = $this->getRequest();
var_dump($_SERVER['REQUEST_METHOD']);
if ($request->isPost()) {
$del = $request->getPost('delete_confirmation', 'no');
if ($del === 'yes') {
$this->postService->deletePost($post);
}
return $this->redirect()->toRoute('post');
}
return new ViewModel(array(
'post' => $post
));
}
}
ZendDbSqlMapper.php(保存并删除映射部分)
namespace Blog\Mapper;
use Blog\Model\PostInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\HydratingResultSet; /*used directly below*/
use Zend\Db\Sql\Delete;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Update;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Db\Sql\PreparableSqlInterface;
class ZendDbSqlMapper implements PostMapperInterface{
protected $dbAdapter;
protected $hydrator;
protected $postPrototype;
public function __construct(AdapterInterface $dbAdapter,HydratorInterface $hydrator,PostInterface $postPrototype) {
$this->dbAdapter = $dbAdapter;
$this->hydrator = $hydrator;
$this->postPrototype = $postPrototype;
}
public function save(PostInterface $postObject)
{
$postData = $this->hydrator->extract($postObject);
unset($postData['id']);
if($postObject->getId())
{
$action = new Update('posts');
$action->set($postData);
$action->where(array('id = ?'=>$postObject->getId()));
}
else
{
$action = new Insert('posts');
$action->values($postData);
}
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
if($result instanceof ResultSetInterface)
{
if($newId = $result->getGeneratedValue())
{
$postObject->setId($newId);
}
return $postObject;
}
throw new \Exception('Database error in ZendDbSqlMapper.php');
}
public function delete(PostInterface $postObject)
{
$action = new Delete('posts');
$action->where(array('id = ?' => $postObject->getId()));
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return (bool)$result->getAffectedRows();
}
}
delete.phtml
<h1>DeleteController::deleteAction()</h1>
<p>
Are you sure that you want to delete
'<?php echo $this->escapeHtml($this->post->getTitle()); ?>' by
'<?php echo $this->escapeHtml($this->post->getText()); ?>'?
</p>
<form action="<?php echo $this->url('post/delete', array(), true); ?>" method="post">
<input type="hidden" name="id" value="<?php echo $post->getId(); ?>">
<input type="submit" name="delete_confirmation" value="yes">
<input type="submit" name="delete_confirmation" value="no">
</form>
问题:
当我尝试在 http://www.myzend.com/blog/delete/2
之类的浏览器中删除博客时,它显示
Database error in ZendDbSqlMapper.php
我什么都试过了,但找不到哪里出错了。
我正在使用 Zend framework 2.3.3
如果需要,我可以添加更多详细信息。
我终于找到了 issue.It 在 PostService.php
中,它在 deletePost
中返回 save
方法,这应该是 delete
方法.