cakephp在编辑页面中获取旧记录和新记录

cakephp在编辑页面中获取旧记录和新记录,cakephp,cakephp-2.0,edit,difference,Cakephp,Cakephp 2.0,Edit,Difference,CakePHP2,我有一个编辑页面。我想看看有什么变化。因此,我需要获取$this->request->data。但是,它无法获取未编辑的旧记录和已编辑的新记录。我怎么做?请 public function admin_edit($id = null) { $this->BrandImage->id = $id; if (!$this->BrandImage->exists($id)) { throw new

CakePHP2,我有一个编辑页面。我想看看有什么变化。因此,我需要获取$this->request->data。但是,它无法获取未编辑的旧记录和已编辑的新记录。我怎么做?请

    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'));

    }

修改后的数据可以在$this->request->data中找到,您可以在保存发布的数据之前从数据库中读取旧数据。请查看以下示例:

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'));

}