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?

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

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?