无法将数据从1个表保存到另一个表

时间:2014-10-02 12:52:58

标签: php mysql cakephp

在cakephp中,我无法从1个表中返回的数据保存在另一个表中。 我从Tutors表中预先填充了一个表格中的数据,我想要做的就是将这些数据保存为表tutorEdit中的新行(不与编辑功能混淆)。 我得到的问题是我得到了要保存的数据但是tutorEdit没有保存任何返回的数据(没有错误)。

public function tutor_edit($id = null) {
        $this->loadModel('Tutor');

        $this->Tutor->id = $id;

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

        if (!$this->Tutor->exists()) {
            throw new NotFoundException(__('Invalid tutor'));
        }
        if ($this->request->is('post')  ) {

            if ($this->TutorEdit->save($this->request->data)) {
                $this->Session->setFlash(__('The tutor details to be edited have ben forwarded to management'), 'flash_success');
               // $this->redirect(array('controller'=> 'tutors' , 'action' => 'tutordetails'));
            } else {
                $this->Session->setFlash(__('The tutor edit details could not be saved. Please, try again.'), 'flash_alert');
            }



        } else {
            $this->request->data = $this->Tutor->read(null, $id);
        }

/////
   <?php
                    echo $this->Form->create('Tutor',array('class' => 'form-horizontal'));
                    echo $this->Form->input('id', $formHorizontalHtmlOptions);
                    echo $this->Form->input('first_name', $formHorizontalHtmlOptions);
                    echo $this->Form->input('last_name', $formHorizontalHtmlOptions);
                    echo $this->Form->input('email', $formHorizontalHtmlOptions);
                    echo $this->Form->input('mobile', $formHorizontalHtmlOptions);
                    echo $this->Form->input('home_phone', $formHorizontalHtmlOptions);
                    echo $this->Form->input('work_phone', $formHorizontalHtmlOptions);
                    echo $this->Form->input('gender', array_merge($formHorizontalHtmlOptions, array('type' => 'select', 'options' => $gender)));
                    echo $this->Form->end('Save Edit Request');   
                    ?>

http://book.cakephp.org/2.0/en/models/saving-your-data.html

中没有看到任何相关内容

1 个答案:

答案 0 :(得分:1)

因为您要保存的数据是&#39; Tutor&#39;,而不是&#39; TutorEdit&#39;。在您分享的链接中,第一部分显示了需要保存的正确数组格式。

试试这个:

if ($this->request->is('post')  ) {
    $tutoredit = array('TutorEdit' => $this->request->data['Tutor']);

    if ($this->TutorEdit->save($tutoredit)) {
            $this->Session->setFlash(__('The tutor details to be edited have ben forwarded to management'), 'flash_success');
    } else {
            $this->Session->setFlash(__('The tutor edit details could not be saved. Please, try again.'), 'flash_alert');
    }
}