2017-09-16 6 views
0

CakePHP 2, j'ai une page d'édition. Je voudrais voir ce qui a été changé. Par conséquent, j'ai besoin de $ this-> request-> data. Cependant, il ne parvient pas à récupérer l'ancien enregistrement (non-édition) et le nouvel enregistrement (édité). Comment puis-je le faire? S'il vous plaîtcakephp obtenir l'ancien et le nouveau record dans la page d'édition

public function admin_edit($id = null) { 
     $this->BrandImage->id = $id; 
     if (!$this->BrandImage->exists($id)) { 
      throw new NotFoundException(__('Invalid brand image')); 
     } 

     $old_content = array(); 
     $new_content = array(); 

     ***debug($this->request->data);*** 

     if ($this->request->is('post') || $this->request->is('put')) { 
      if ($this->BrandImage->save($this->request->data)) { 

       ***debug($this->request->data);*** 

       $this->Session->setFlash(__('The brand image has been saved'), 'flash/success'); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The brand image could not be saved. Please, try again.'), 'flash/error'); 
      } 
     } else { 
      $options = array('conditions' => array('BrandImage.' . $this->BrandImage->primaryKey => $id)); 
      $this->request->data = $this->BrandImage->find('first', $options); 
     } 
     $brands = $this->BrandImage->Brand->find('list'); 
     $imageTypes = $this->BrandImage->ImageType->find('list'); 
     $this->set(compact('brands', 'imageTypes')); 

    } 

Répondre

1

Les données modifiées se trouvent dans $ this-> request-> données, et vous pouvez lire les anciennes données de la base de données avant d'enregistrer les données affichées. Veuillez trouver l'exemple ci-dessous:

public function admin_edit($id = null) { 
    $this->BrandImage->id = $id; 
    if (!$this->BrandImage->exists($id)) { 
     throw new NotFoundException(__('Invalid brand image')); 
    } 

    $old_content = array(); 
    $new_content = array(); 

    ***debug($this->request->data);*** 


    if ($this->request->is('post') || $this->request->is('put')) { 

     /* This is the old data read from the database before the save */ 
     $old_content = $this->BrandImage->find('first', array(
     'conditions' => array(
      'BrandImage.id' => $id 
       ) 
     )); 

     debug($old_content); 

     if ($this->BrandImage->save($this->request->data)) { 

      ***debug($this->request->data);*** 

      $this->Session->setFlash(__('The brand image has been saved'), 'flash/success'); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The brand image could not be saved. Please, try again.'), 'flash/error'); 
     } 
    } else { 
     $options = array('conditions' => array('BrandImage.' . $this->BrandImage->primaryKey => $id)); 

     $this->request->data = $this->BrandImage->find('first', $options); 
    } 
    $brands = $this->BrandImage->Brand->find('list'); 
    $imageTypes = $this->BrandImage->ImageType->find('list'); 
    $this->set(compact('brands', 'imageTypes')); 

}