从db中删除数据后刷新页面

时间:2014-06-09 17:14:26

标签: cakephp cakephp-2.4

我有动作从我的应用中的图库中删除图像。当用户单击“删除”按钮时,图像将被删除并显示“成功”消息,但删除的图像仍在图像列表中,一旦按下刷新它就会消失。

如何在用户按下删除按钮后立即使该图像消失?

我已尝试使用$this->redirect('/Admin/Dashboard/Gallery/Delete');,但不允许显示“成功”消息。

我正在使用CakePHP 2.4.4。

控制器

public function deleteImages($id){
        $this->set('title_for_layout', 'Apagar imagens');
        $this->layout = 'admin';
        $this->loadModel('GalleryImage');
        $this->GalleryImage->id=$id;
        $this->Paginator->settings = array(
                'GalleryImage' => array(
                    'limit' => 20,
                    //'maxLimit' => 100,
                    'order' => array('GalleryImage.modified' => 'desc') // Por exemplo
                )
            );
        $gallery_images=$this->Paginator->paginate('GalleryImage');
        $this->set('gallery_images', $gallery_images);
        if($this->request->is('post')){
            if(!$this->GalleryImage->exists()){
                throw new NotFoundException('Erro, esta fotografia não foi encontrada.', 'default', array('class'=>'alert flashMessageDanger alert-dismissable'));
            }
            $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']['name'])){
                unlink(WWW_ROOT."img/Gallery/".$gallery_image_delete['GalleryImage']['name']);
                $this->GalleryImage->delete();
                $this->Session->setFlash('A Imagem foi excluída com sucesso.', 'default', array('class'=>'alert flashMessageSuccess alert-dismissable'));
                $this->redirect('/Admin/Dashboard/Gallery/Delete');
            }else{
                $this->Session->setFlash('Erro, esta Imagem não existe.', 'default', array('class' => 'alert flashMessageDanger alert-dismissable'));
            }
            //$this->redirect('/Admin/Dashboard/Gallery/Delete');
        }
    }

1 个答案:

答案 0 :(得分:1)

更改代码的顺序,以便在您的代码实际执行删除之后,显示在页面上显示图库图像的行$gallery_images = $this->Paginator->paginate('GalleryImage');

public function deleteImages($id){
    $this->set('title_for_layout', 'Apagar imagens');
    $this->layout = 'admin';
    $this->loadModel('GalleryImage');
    $this->GalleryImage->id=$id;
    //code moved from here.

    if($this->request->is('post')){
        if(!$this->GalleryImage->exists()){
            throw new NotFoundException('Erro, esta fotografia não foi encontrada.', 'default', array('class'=>'alert flashMessageDanger alert-dismissable'));
        }
        $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']['name'])){
            unlink(WWW_ROOT."img/Gallery/".$gallery_image_delete['GalleryImage']['name']);
            $this->GalleryImage->delete();
            $this->Session->setFlash('A Imagem foi excluída com sucesso.', 'default', array('class'=>'alert flashMessageSuccess alert-dismissable'));
            $this->redirect('/Admin/Dashboard/Gallery/Delete');
        }else{
            $this->Session->setFlash('Erro, esta Imagem não existe.', 'default', array('class' => 'alert flashMessageDanger alert-dismissable'));
        }
        //$this->redirect('/Admin/Dashboard/Gallery/Delete');
    }

    //code moved to here.
    $this->Paginator->settings = array(
            'GalleryImage' => array(
                'limit' => 20,
                //'maxLimit' => 100,
                'order' => array('GalleryImage.modified' => 'desc') // Por exemplo
            )
        );
    $gallery_images=$this->Paginator->paginate('GalleryImage');
    $this->set('gallery_images', $gallery_images);
}