【问题标题】:Refresh page after deleting data from db从数据库中删除数据后刷新页面
【发布时间】:2014-06-09 17:14:25
【问题描述】:

我有一个从我的应用程序的画廊中删除图像的操作。当用户单击“删除”按钮时,图像被删除并显示“成功”消息,但删除的图像仍在图像列表中,一旦我按刷新它就消失了。

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

我已经尝试过使用$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');
        }
    }

【问题讨论】:

    标签: cakephp cakephp-2.4


    【解决方案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);
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      相关资源
      最近更新 更多