不能将数据从1个表保存到另一个表 [英] cant save data from 1 table to another table

查看:82
本文介绍了不能将数据从1个表保存到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In cakephp I cant get the data returned from 1 table to be saved in another table. I have data pre-populated in a form from the Tutors table and all I want to do is save this data as a new row in table tutorEdit (not confused with an edit function). The issue I get is that I get the data to save but tutorEdit doesnt save any of the data returned (no error).

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。在您分享的链接中,第一部分显示需要保存的正确数组格式。

Because the data you are trying to save is 'Tutor', not 'TutorEdit'. In that link you shared, the first section shows the proper array format that needs to be saved.

尝试:

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

这篇关于不能将数据从1个表保存到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆