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:
I press on the button Delete on the row i want to delete.
I press on Yes Delete1
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.
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?