dongzecai0684 2016-01-04 13:10
浏览 79

ZF2 deleteAction不起作用

public function deleteAction() {

    $id = (int) $this->params()->fromRoute('id',0);
    if (!$id) {
        return $this->redirect()->toRoute('studfood');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $del = $request->getPost('del', 'No');

        if ($del == 'Yes') {
            $id = (int) $request->getPost('id');


        return $this->redirect()->toRoute('studfood');

    return array(
            'id'         => $id,
            'studfood'   => $this->getRezepteTable()->getRezepte($id)
    ); }

As you can see that is my deleteAction in my "RezepteController". I actually copied the album tutorial from zend2.

namespace Studfood\Model;

       use Zend\Db\TableGateway\TableGateway;
       use Studfood\Model\Rezepte;
       use Zend\Db\Sql\Sql;
       use Zend\Db\Adapter\Adapter;

class RezepteTable {

protected $tableGateway;

public function __construct(TableGateway $tableGateway) {

    $this->tableGateway = $tableGateway;
public function deleteRezepte($id) {

        if($id != 0) {
        $this->tableGateway->delete(array('id' => (int) $id)); }
        else { throw new \Exception('id = 0'); }
    } }

And thats my RezepteTable with the database command in it.

So my problem is that if i want to Delete something and press on the button "Yes", nothing happens. Thats why I added this Exception to the RezepteTable. And I always get the id=0 Exception.

The only difference from the tutorial is, that i added more tables to my project with Forgein Keys in it. (fetchall with joints etc.).

 use Studfood\Model\Rezepte;

 $name = 'Rezepte loeschen';
 <h1><?php echo $this->escapeHtml($name); ?></h1>

 p>Are you sure that you want to delete
 '<?php echo $this->escapeHtml($rezepte->name); ?>'?
 $url = $this->url('studfood', array(
 'action' => 'delete',
 'id'     => $this->id,
   <form action="<?php echo $url; ?>" method="post">
 <input type="hidden" name="id" value="<?php echo (int) $rezepte->id; ?>" />
 <input type="submit" name="del" value="Yes" />
 <input type="submit" name="del" value="No" />

And thats finally my delete View.

So what happens is following:

  1. I press on the button Delete on the row i want to delete.

  2. I press on Yes Delete1

  3. I get following Error because of the Exception in deleteRezepteDelete2

Without the Exception it will just redirect me to my main page.

So i have no clue where my error is. Somebody got any idea?

Much appreciated.

  • 写回答

1条回答 默认 最新

  • dongyue9864 2016-01-06 09:32

    You get id from post. Can you not use id from route?

    In your view you use two different ways to output id $this->id and $rezepte->id. Are you sure both contain the correct value?

    I think you should check the value on the hidden id field in your form?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值