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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀