简体   繁体   中英

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

I am new to ZF2 application. I trying the documentation part as n practice provided in ZF2 doc. Working on album module it is find with the other parts but as it comes to delete. My delete album part is not working and also not showing any error as well My files for delete code as

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

Other Album Controller file is as

//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),
        );

    }
}

Deleting File view as follows

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

When i'm trying to delete the album it askes for confirmation and when pressed as yes it get to album lists again showing the same record within the lists.

Maybe you are having problem with the first files itself for delete operation

In your //src/Album/Model/AlbumTable.php file

You stated the deleteAlbum function as

public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array($id => (int) $this->id));
    }

Rewrite the code as

public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array('id' => (int) $id));
    }

Hopefullly this was the only problem as you said it doesn't showing any error or at least exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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