Zend F2骨架应用程序'的相册模块'的删除操作不工作


Zend F2 Skeleton application's album module's delete action not working

我是ZF2应用的新手。我尝试文档部分作为ZF2文档中提供的实践。工作在专辑模块,它是发现与其他部分,但作为它来删除。我的删除相册部分不工作,也没有显示任何错误,以及我的文件删除代码为

//src/Album/Model/AlbumTable.php
namespace  Album'Model;
use Zend'Db'TableGateway'TableGateway;
Class AlbumTable
{
    protected $tableGateway;
    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }
    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    public function getAlbum($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array('id'=> $id));
        $row = $rowset->current();
        if(!$row)
        {
            throw new Exception("Could not find row - $id");
        }
        return $row;
    }
    public function saveAlbum(Album $album)
    {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );
        $id = (int) $album->id;
        if($id == 0)
        {
            $this->tableGateway->insert($data);
        }
        else
        {
            if($this->getAlbum($id))
            {
                $this->tableGateway->update($data, array('id' => $id));
            }
            else
            {
                throw new 'Exception("Album ID does not exists");
            }
        }
    }
    public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array($id => (int) $this->id));
    }
}

其他专辑控制器文件为

//src/Album/Controller/AlbumController.php
namespace Album'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Model'ViewModel;
use Album'Model'Album;          // For the Form Display
use Album'Form'AlbumForm;      // album manipulation form view
Class AlbumController extends AbstractActionController
{
    protected $albumTable;
    public function getAlbumTable()
    {
        if(!$this->albumTable)
        {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album'Model'AlbumTable');
        }
        return $this->albumTable;
    }

    public function indexAction() 
    {
        //Albums Retreiving from Model and Passing to View for listing the albums
        return new ViewModel(array(
            'albums' => $this->getAlbumTable()->fetchAll(),
        ));
    }
    public function addAction()
    {
        $form = new AlbumForm();
        $form->get('submit')->setValue('Add');
        $request = $this->getRequest();
        if($request->isPost())
        {
            $album = new Album();
//            printf($form);die();
            $form->setInputFilter($album->getInputFilter());    
            $form->setData($request->getPost());
            if($form->isValid())
            {
                $album->exchangeArray($form->getData());
                $this->getAlbumTable()->saveAlbum($album);
                //Redirect to list of albums
                $this->redirect()->toRoute('album');
            }
        }
        return array('form' => $form);
    }
    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id',0);
        if(!$id)
        {
            return $this->redirect()->toRoute('album',array(
                'action' => 'add'
            ));
        }
         // Get the Album with the specified id.  An exception is thrown
         // if it cannot be found, in which case go to the index page.
        try
        {
            $album = $this->getAlbumTable()->getAlbum($id);
        }
        catch('Exception $ex)
        {
            return $this->redirect()->toRoute('album', array(
                 'action' => 'index'
             ));
        }
        $form = new AlbumForm();
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'Edit');
        $request = $this->getRequest();
        if($request->isPost())
        {
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            if($form->isValid())
            {
                $this->getAlbumTable()->saveAlbum($album);
                //Redirect to list all albums
                return $this->redirect()->toRoute('album');
            }
        }
        return array(
            'id' => $id,
            'form' => $form,
        );
    }
    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id',0);
        if(!$id)
        {
            return $this->redirect()->toRoute('album');
        }
        $request = $this->getRequest();
        if($request->isPost())
        {
            $del = $request->getPost('del','No');
            if($del=='Yes')
            {
                $id = (int)$request->getPost('id');
                $this->getAlbumTable()->deleteAlbum($id);
            }
            //Redirect to list of Albums
            return $this->redirect()->toRoute('album');
        }
        return array(
            'id'    => $id,
            'album' => $this->getAlbumTable()->getAlbum($id),
        );
    }
}

按如下命令删除文件视图

//view/album/album/delete.phtml
$title = "Delete Album";
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>
    Are you sure want to delete the album
    '<?php echo $this->escapeHtml($album->title); ?>' By 
    '<?php echo $this->escapeHtml($album->artist); ?>' ??
</p>
<?php
    $url = $this->url('album', array(
        'action' => 'delete',
        'id'     => $this->id,
    ));
?>
<form action='<?php echo $url; ?>' method='post'>
    <div>
        <input type='hidden' name='id' value='<?php echo (int)$album->id; ?>'/>
        <input type='submit' name='del' value='Yes'/>
        <input type="submit" name="del" value='No'/>
    </div>
</form>

当我试图删除专辑时,它要求确认,当按下是时,它再次进入专辑列表,在列表中显示相同的记录。

可能是第一个文件本身的删除操作有问题

在你的//src/Album/Model/AlbumTable.php文件

您将deleteAlbum函数声明为

public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array($id => (int) $this->id));
    }
将代码重写为
public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array('id' => (int) $id));
    }

希望这是唯一的问题,因为你说它没有显示任何错误或至少异常。