doumei1926 2016-03-31 18:20
浏览 21

如何使用同一对象在datamapper中添加多行

I know that using $dataMapperObject->query('INSERT INTO table (a,b) VALUES (1,2), (2,3), (3,4);'); will save multiple records as it's simple sql INSERT statement.

But how to store multiple values in same object of datamapper?

Example:

$addPatient= new Patient_table();  
$addPatient->trans_begin();
foreach($_POST as $key=>$val){
    $addPatient->patient_id=$this->patient-id;
    $addPatient->question=$key;
    $addPatient->answer=$val;
    $addPatient->save();
}

Here in above example i want to store each and every question and answer successfully. If any of one is missing i wants to rallback entire transaction.

How to store all question answer in single object call?

  • 写回答

1条回答 默认 最新

  • dousigan0499 2016-03-31 19:16
    关注

    i'm not that familiar with using datamapper but i think the following should work:

    $addPatient= new Patient_table();  
    $addPatient->trans_begin();
    foreach($_POST as $key=>$val){
        $addPatient->patient_id=$this->patient-id;
        $addPatient->question=$key;
        $addPatient->answer=$val;
        $addPatient->save();
    }
    if ($addPatient->trans_status() === FALSE)
    {
        $addPatient->trans_rollback();
    }
    else
    {
        $addPatient->trans_commit();
    }
    

    I think your database fields might have to be set so that they may not contain null, here is another post on the same topic. Can I rollback a transaction after commiting it with Datamapper/Codeigniter?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用