我正在关注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
等浏览器中删除博客时,会显示错误,如
ZendDbSqlMapper.php中的数据库错误
我尝试了一切,但找不到它出错的地方。
我正在使用Zend framework 2.3.3
如果需要,我可以添加更多详细信息。
答案 0 :(得分:1)
我终于找到了问题。它在xf:submission for submission id: fr-default-submission, error code received when submitting instance: 401
PageFlowControllerProcessor - unauthorized {controller: "oxf:/apps/fr/page-flow.xml", method: "GET", path: "/fr/service/persistence/form", status-code: "401"}
,它在PostService.php
内返回save
方法,这应该是deletePost
方法。