简体   繁体   English

ZF2 deleteAction不起作用

[英]ZF2 deleteAction not working

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

            $this->getRezepteTable()->deleteRezepte($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". 如您所见,这是我的“ RezepteController”中的deleteAction。 I actually copied the album tutorial from zend2. 我实际上是从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. 这就是我的RezepteTable中的数据库命令。

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. 这就是为什么我将此异常添加到RezepteTable的原因。 And I always get the id=0 Exception. 而且我总是得到id = 0异常。

The only difference from the tutorial is, that i added more tables to my project with Forgein Keys in it. 与本教程的唯一区别是,我在其中添加了带有Forgein Keys的项目表。 (fetchall with joints etc.). (带有关节等)。

 <?php
 use Studfood\Model\Rezepte;

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

 p>Are you sure that you want to delete
 '<?php echo $this->escapeHtml($rezepte->name); ?>'?
 </p>
 <?php
 $url = $this->url('studfood', array(
 'action' => 'delete',
 'id'     => $this->id,
  ));
   ?>
   <form action="<?php echo $url; ?>" method="post">
    <div>
 <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" />
  </div>
  </form>

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 我按是删除1

  3. I get following Error because of the Exception in deleteRezepte Delete2 由于deleteRezepte Delete2中的异常,我得到以下错误

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. 非常感激。

You get id from post. 您从帖子中获得ID。 Can you not use id from route? 不能从路线使用ID?

In your view you use two different ways to output id $this->id and $rezepte->id . 在您看来,您使用两种不同的方式来输出id $this->id$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? 我认为您应该检查表单中隐藏id字段上的value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM