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

            $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". 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.).

 <?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

  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?

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路