I need to get the id
of the link present in the postLink
method to be able to delete an image whose id
is equal to that id of the link, which is equal to the id of the database.
How do I get this value?
View
<h2>Apagar Fotografia</h2>
<br>
<table border="1" bordercolor="#e2e2e2" width="720" style="word-wrap: break-word">
<tr>
<?php
$i=0;
foreach( $gallery_images as $gallery_image ):?>
<?php
echo "<td style=text-align: justify>";
//echo $gallery_image['GalleryImage']['path'];
echo $this->Form->postLink('Apagar Fotografia', array('controller'=>'Gallery', 'action'=>'admin_del_image', $gallery_image['GalleryImage']['id'],/*'prefix'=>'admin'*/), array('class'=>'foto_del', 'title'=>'Apagar Fotografia'), __('Tem a certeza que quer apagar esta Fotografia?'));
echo "</td>";
echo "<td>";
//$src3 =$this->webroot. 'img/gallery/' .$gallery_image['GalleryImage']['path'];
echo $this->Html->image('gallery/' . $gallery_image['GalleryImage']['path'] , array('width' => '200px', 'height' => '133px', 'alt' => $gallery_image['GalleryImage']['path'] ));
echo "</td>";
$i++;
if($i==2){
echo "</tr><tr>";
$i=0;
}
?>
<?php endforeach ?>
</tr>
Controller
public function admin_del_image(){
$this->layout = 'admin_index';
$this->loadModel('GalleryImage');
$this->GalleryImage->id=$id;
$gallery_images = $this->GalleryImage->find('all');
$this->set('gallery_images', $gallery_images);
if($this->request->is('post')){
if(!$this->GalleryImage->exists()){
throw new NotFoundException(__('Fotografia inválida'));
}
//$this->request->onlyAllow('post','delete');
$options = array('conditions' => array('GalleryImage.' .$this->GalleryImage->primaryKey=>$id));
$gallery_image_delete=$this->GalleryImage->find('first', $options);
if(file_exists(WWW_ROOT. "img/gallery/" .$gallery_image_delete['GalleryImage']['path'])){
unlink(WWW_ROOT . "img/gallery/" .$gallery_image_delete['GalleryImage']['path']);
$this->GalleryImage->delete();
$this->Session->setFlash(__('Fotografia excluída com sucesso'));
}
else{
$this->Session-setFlash(__('ERRO!, esta Fotografia não existe'));
}
$this->redirect($this->refere());}
}
Output
<td style="text-align:" justify=""><form action="/html/PushUp_app/Gallery/admin_del_image/25" name="post_5330531636e39982291469" id="post_5330531636e39982291469" style="display:none;" method="post"><input type="hidden" name="_method" value="POST"></form><a href="#" class="foto_del" title="Apagar Fotografia" onclick="if (confirm("Tem a certeza que quer apagar esta Fotografia?")) { document.post_5330531636e39982291469.submit(); } event.returnValue = false; return false;">Apagar Fotografia</a></td>
<td><img src="/html/PushUp_app/img/gallery/PushUp.png" width="200px" height="133px" alt="PushUp.png"></td>