dougao1542 2014-02-08 07:53
浏览 52
已采纳

cakephp,如何在两个表中同时保存数据

i have two tables- users and employee_personals. I want to insert employee_personal.epf_no to users table at the same time when inserting employee_personal data.my code is as below

    if ($this->request->is('post')) 
    {
       $this->EmployeePersonal->create();
       $post_data = $this->request->data;
       if ($this->EmployeePersonal->save($post_data)) 
       {

          $data=$this->request->data['EmployeePersonal']['epf_no'];
          if ($this->User->save($data))
          {   
            $this->redirect(array('controller' =>'employee_personals','action'=>'add'));
          }

    }
$this->Session->setFlash(__('Unable to save personal details'));
}

When i submit the form then employee_personals is inserted, but user.epf_no is empty.pls hel me to write the function

  • 写回答

1条回答 默认 最新

  • dongyong6045 2014-02-08 09:25
    关注

    Like the other answer/comment, using saveAll() or saveAssociated() is preferable. However, if you want to proceed with your code, you need to change $data. The following should work:

    $data['User']['epf_no'] = $this->request->data['EmployeePersonal']['epf_no'];
    if ($this->User->save($data)) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline