dongxingguo1978 2012-05-07 21:42
浏览 48
已采纳

cakephp:使用saveAll(),导入(非表单相关)关联数据不保存

My question relates to pulling data from a remote source and saving it to a local database. With the saveAll(), the parent data gets saved correctly, but not the child data.

protected function _saveLocal($_rekeyedData) {
        //Set the invoice header and line items models
        if (!isset($this->LocalHeader)) {
            $this->loadModel('LocalHeader');
            $this->LocalHeader->create();
//            $this->LocalHeader->set($_rekeyedData);
            if (!isset($this->LocalDetail)) {
                $this->loadModel('LocalDetail');
                $this->LocalDetail->create();
            }
        }

        if ($this->LocalHeader->saveAll($_rekeyedData, array('validate' => 'first'))) {
            $this->Session->setFlash('Your data has been saved.');
        } else {
            $this->Session->setFlash('Data load failed.');
        }
        debug( $this->LocalDetail->invalidFields() );
    }

In this function, I load the models, then attempt to saveAll(). A sample record looks like this:

[1] => Array
    (
        [LocalHeader] => Array
            (
                [SOPNUMBE] => CR014076             
                [DUEDATE] => 2012-04-10 00:00:00
                [DOCDATE] => 2012-04-10 00:00:00
                [DOCAMNT] => 12000.00000
                [SUBTOTAL] => 12000.00000
            )

        [LocalDetail] => Array
            (
                [0] => Array
                    (
                        [ITEMNMBR] => BASIC SERVICE                  
                        [QUANTITY] => 1.00000
                        [UOFM] => EA       
                        [UNITPRCE] => 12000.00000
                        [TAXAMNT] => .00000
                        [CONTSTARTDTE] => 1900-01-01 00:00:00
                        [CONTENDDTE] => 1900-01-01 00:00:00
                        [SOPNUMBE] => CR014076             
                    )

            )

    )

All that gets saved is the LocalHeader record. My hunch is that it has something to do with the array('validate' => 'first'). I think I need to do something separate like validateMany when using saveAll(), but I can't remember where I saw something like that or if I just made it up.

I've read many of the related questions in SO, but none of the offered solutions worked for me.

Any help is appreciated. Please feel free to ask if you have any questions.

EDIT: This is the header model and this is the detail model.

  • 写回答

1条回答 默认 最新

  • donglu8779 2012-05-08 01:23
    关注

    I think the issue is that neither of those records exists yet. According to the docs

    If neither of the associated model records exists in the system yet (for example, you want to save a new User and their related Profile records at the same time), you’ll need to first save the primary, or parent model.

    You can read more here with an example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来