Zend F2 Skeleton应用程序的相册模块的删除操作不起作用

时间:2015-08-21 12:44:39

标签: php zend-framework zend-framework2

我是ZF2应用程序的新手。我尝试将文档部分作为ZF2 doc中提供的n实践。在相册模块上工作,可以找到其他部分但是要删除它。我的删除相册部分无效,也没有显示任何错误 我的删除代码文件为

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

当我试图删除相册时,它会要求确认,当按下时,它会再次显示相册列表中的相同记录。

1 个答案:

答案 0 :(得分:1)

也许您对删除操作的第一个文件本身有问题

在你的//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));
    }

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