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

标签 php mysql cakephp

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

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 中没有看到任何有关此内容的信息

最佳答案

因为您尝试保存的数据是“Tutor”,而不是“TutorEdit”。在您共享的链接中,第一部分显示了需要保存的正确数组格式。

试试这个:

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

关于php - 无法将数据从一个表保存到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26161251/

相关文章:

PHP MYSQL 基于变量显示表列

php - 如何创建 DRY 工作流程以将电话号码与用户相关联

php - 如何在cakephp2中运行普通的sql查询并获取结果

php - 非拉丁字符和哎哟

php - 我如何访问 CakePHP 的 app.php 中的 SESSION?

php - namespace 错误。卡在 'MyApp\Chat'

php - Ajax 发布到数据库不发布任何内容

mysqli - undefined variable : mysqli - PHP OOP

mysql - 数据库设计 : users and guests

mysql - 高效获取两列总和最高的 25 行 (MySQL)