SaveMany 在 cakePHP 中同时更新多条记录不起作用

2023-12-14

我面临着使用 saveMany 同时更新多个记录的问题,我有如下关联:

  • 候选人hasMany候选人雇主
  • 候选人雇主属于候选人

Candidate.php 中的模型关联:

public $hasMany = array(
        'CandidatesEmployer' => array(
            'className' => 'CandidatesEmployer',
            'foreignKey' => 'candidate_id'
        )
}

这是方法CandidatesController:

public function jbseeker_empdetails() {
        $this->layout = 'front_common';

        $Employers = $this->Candidate->CandidatesEmployer->find('all', array(
            'conditions' => array(
                'candidate_id = ' => $this->Auth->user('id')
            ),
            'recursive' => -1
        ));

        $this->set('Employers', $Employers);

        $this->set('data', $this->request->data);

        if ($this->request->is('post') && !empty($this->request->data)):


            if ($this->Candidate->CandidatesEmployer->saveMany($this->request->data)):
                $this->Session->setFlash('You Employers details has been successfully updated');
                return $this->redirect(array(
                            'controller' => 'candidates',
                            'action' => 'jbseeker_dashboard'
                ));
            else:
                $this->Session->setFlash('You Employers details has not been '
                        . 'updated successfully, please try again later!!');
            endif;

        endif;
}

jbseeker_empdetails.ctp

<h1>Enter the Employers details</h1>

<?php
if (empty($Employers)):

    echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form'));

    echo $this->Form->input('CandidatesEmployer.0.candidate_id', array(
        'type' => 'hidden',
        'value' => $userId
    ));

    echo $this->Form->input('CandidatesEmployer.0.employer');

    echo $this->Form->input('CandidatesEmployer.0.from_year', array(
        'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1))
    ));

    echo $this->Form->input('CandidatesEmployer.0.from_month', array(
        'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1))
    ));

    echo $this->Form->input('CandidatesEmployer.0.to_year', array(
        'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1))
    ));

    echo $this->Form->input('CandidatesEmployer.0.to_month', array(
        'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1))
    ));

    echo $this->Form->input('CandidatesEmployer.0.position_type');

    echo $this->Form->input('CandidatesEmployer.0.headline');

    echo $this->Form->input('CandidatesEmployer.0.designation');

    echo $this->Form->input('CandidatesEmployer.0.role');

    echo $this->Form->input('CandidatesEmployer.0.annual_salary_lakhs');

    echo $this->Form->input('CandidatesEmployer.0.annual_salary_thousands');

    echo $this->Form->input('CandidatesEmployer.0.position_summary');

    echo $this->Form->input('CandidatesEmployer.0.notice_period');

    echo $this->Form->input('CandidatesEmployer.0.job_profile');

    echo $this->Form->input('CandidatesEmployer.0.created_on', array(
        'type' => 'hidden',
        'value' => date('Y-m-d H:i:s')
    ));

    echo $this->Form->input('CandidatesEmployer.0.created_ip', array(
        'type' => 'hidden',
        'value' => $clientIp
    ));

    echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button'));

    echo $this->Form->end();
    ?>

    <button class="add_more">Add more</button>

    <!-- At the end script -->
    <script type="text/javascript">
        var i = 1;
        $(".add_more").click(function () {
            $.ajax({
                url: "
    <?php
    echo $this->Html->url(array(
        $prefixUsed => FALSE, 'jbseeker' => TRUE,
        'controller' => 'candidates',
        'action' => 'jbseeker_generate_emp_form'))
    ?> / " + i,
                        type: 'GET',
                success: function (result) {
                    $('.dynamic_field_form').append(result);
                }});
            i++;
        });
    </script>

<?php else: ?>
    <?php
    echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form'));
    $count = 0;
    foreach ($Employers as $employer):

        echo $this->Form->input('CandidatesEmployer.' . $count . '.id', array(
            'type' => 'hidden',
            'value' => $employer['CandidatesEmployer']['id']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.candidate_id', array(
            'type' => 'hidden',
            'value' => $userId
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.employer', array('value' => $employer['CandidatesEmployer']['employer']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.from_year', array(
            'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)),
            'default' => $employer['CandidatesEmployer']['from_year']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.from_month', array(
            'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)),
            'default' => $employer['CandidatesEmployer']['from_month']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.to_year', array(
            'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)),
            'default' => $employer['CandidatesEmployer']['to_year']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.to_month', array(
            'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)),
            'default' => $employer['CandidatesEmployer']['to_month']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.position_type', array('value' => $employer['CandidatesEmployer']['position_type']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.headline', array('value' => $employer['CandidatesEmployer']['headline']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.designation', array('value' => $employer['CandidatesEmployer']['designation']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.role');

        echo $this->Form->input('CandidatesEmployer.' . $count . '.annual_salary_lakhs', array(
            'options' => array_combine(range(10, 90, 10), range(10, 90, 10)),
            'default' => $employer['CandidatesEmployer']['annual_salary_lakhs']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.annual_salary_thousands', array(
            'options' => array_combine(range(10, 90, 10), range(10, 90, 10)),
            'default' => $employer['CandidatesEmployer']['annual_salary_thousands']
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.position_summary', array('value' => $employer['CandidatesEmployer']['position_summary']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.notice_period', array('value' => $employer['CandidatesEmployer']['notice_period']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.job_profile', array('value' => $employer['CandidatesEmployer']['job_profile']));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.updated_on', array(
            'type' => 'hidden',
            'value' => date('Y-m-d H:i:s')
        ));

        echo $this->Form->input('CandidatesEmployer.' . $count . '.updated_ip', array(
            'type' => 'hidden',
            'value' => $clientIp
        ));
        echo "<hr>";
        ?>
        <?php
        $count++;
    endforeach;
    ?>

    <?php
    echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button'));
    echo $this->Form->end();
    ?>
    <br>
    <?php
    echo $this->Html->link('Add another Employer', array(
        'controller' => 'candidates',
        'action' => 'jbseeker_addemployer'
    ));
    ?>
<?php
endif;

请求数据 here


这是因为您定义表单字段的方式。

蛋糕书saveMany指出数据应位于“数字索引而不是文章键”中。http://book.cakephp.org/2.0/en/models/ saving-your-data.html#model-savemany-array-data-null-array-options-array

因此,您的数据是数字索引,但位于 CandidatesEmployer 键下,因此saveMany函数没有看到任何要保存的数据,因此没有错误。

将您的保存行更改为此应该有效:

$this->Candidate->CandidatesEmployer->saveMany($this->request->data['CandidatesEmployer'])

这样只传递数字索引。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SaveMany 在 cakePHP 中同时更新多条记录不起作用 的相关文章

随机推荐

  • pytorch 中的 reshape 和 view 有什么区别?

    在 numpy 中 我们使用ndarray reshape 用于重塑数组 我注意到在 pytorch 中 人们使用torch view 出于同样的目的 但同时 还有一个torch reshape 现存的 所以我想知道它们之间有什么区别以及何
  • 在 JavaScript 中返回 !1

    我刚刚在 JavaScript 中遇到了一个函数 它有return 1 这实际上意味着什么 你为什么要return 1 or return 0 这是我遇到的函数 function convertStringToBoolean a typeo
  • let 语句中的 case 语句需要什么缩进?

    在 haskell 中工作 发现奇怪的行为 将其精简为简单的框架 这有效 a Bool a case True of True gt True False gt False 但当我尝试时 b IO Bool b do let b case
  • 我备份了一个 git 项目,并得到“致命:不是 Git 存储库”

    我将 Linux 上的 git 文件夹备份到 FAT32 外部驱动器上 稍后将其复制回来 它说 fatal Not a git repository or any of the parent directories git 可能出了什么问题
  • 错误:变量可能尚未初始化

    收到错误 Pay java 81 错误 变量hourlyWage可能不是 已初始化JOptionPane showMessageDialog null hourlyWage 对于以下代码 双hourlyWage已声明 这些陈述位于主要论点内
  • 应用程序处于后台模式时的文本到语音功能?

    我正在研究一个TextToSpeech应用程序 我在一篇文章中写了一段UITextField 然后我按Speak按钮 声音根据写入的文本播放UITextField 但是 当应用程序处于后台模式时 音频将停止播放 如何在后台模式下继续播放声音
  • Angularjs如何上传多部分表单数据和文件?

    我是 angular js 的初学者 但我很好地掌握了基础知识 我想要做的是将文件和一些表单数据作为多部分表单数据上传 我读到这不是 Angular 的功能 但是第 3 方库可以完成此任务 我已经通过 git 克隆了 Angular fil
  • 在循环中为 Tkinter Entry 小部件创建 StringVar 变量

    我有一个小脚本 可以生成随机数量的条目小部件 每个都需要一个 StringVar 以便我可以将文本分配给小部件 由于我无法提前知道会有多少个 因此如何将它们创建为循环的一部分 from Tkinter import import rando
  • Pygame 应用程序中的 SVG 渲染。在 Pygame 2.0 之前,Pygame 不支持 SVG。那你是怎么加载的呢?

    In a Pygame应用程序中 我想渲染 SVG 中描述的无分辨率 GUI 小部件 我怎样才能实现这个目标 我喜欢OCEMP图形用户界面工具包 但它的渲染似乎依赖于位图 这是一个完整的例子 结合了其他人的提示 它应该从当前目录渲染一个名为
  • 如何将 TextView 文本颜色设置为特定主题颜色

    我尝试学习 Android 主题 但在设置主题时遇到了麻烦TextViewTextColor 为另一种颜色 然后是全局的
  • Delphi:Char 和 TCharArray“不兼容类型”数组

    我在下面的评论中遇到过几次 不兼容的类型 错误 并且一直对为什么 Delphi 2007 不直接支持此错误感到满意 program Project1 APPTYPE CONSOLE type TCharArray array of Char
  • 查找图像的方向

    我正在使用 OpenCv 进行模式匹配 我有一个模型 我将目标与函数 cvMatchShapes 进行比较 它有效 但我想知道目标的方向 我该怎么做 例如 边界旋转矩形是否适合轮廓方向相差 180 度的情况 解决问题的另一种方法是计算轮廓矩
  • 从另一个文件获取变量 - python

    我正在创建一个 Tkinter 程序 允许用户在一个漂亮的框中输入文本 而不是在 python shell 中 因为我想在多个程序中使用它 所以我将其制作成一个可以在其他文件中使用的函数 我可以让它在另一个文件中运行 但不能导入变量 这是我
  • PHP中相对路径还是绝对路径以及如何设置

    我正在开发一个网站 我正在尝试访问该网站的主目录 但我不知道如何进行设置 我想做的是包括 inc config php通过 inc config php而不必使用 inc config php有什么想法如何添加这个吗 一般来说 处理路径时需
  • OpenXML 从 Excel 创建数据表 - 货币单元格值不正确

    我正在尝试使用 OpenXML 从 Excel 电子表格创建数据表 使用 Cell CellValue innerXml 获取行的单元格值时 为用户输入的货币值返回的值 在电子表格上可见 与解释的值不同 电子表格单元格格式为文本 单元格值为
  • 在 iOS 10+ 中,有什么方法可以可靠地唤醒应用程序

    我已经这样做了三个多月了 我的头发都被拔掉了 所以请不要回复初学者的答案 我想知道 在 2017 年的 iOS 10 中 是否有任何方法可以将应用程序从终止状态唤醒 最好是通过蓝牙外围设备 但我会采取我能得到的 我认为终止的时间是用户在任务
  • 如何比较签名和未签名(并避免问题)

    最近我听说 C 中的有符号 无符号比较可能很棘手 例如 有符号 无符号比较还有其他一些问题 我的问题是 如果我们必须将有符号类型与无符号类型进行比较 例如 包括 gt 存在哪些策略可以避免这种比较产生的问题 或者我们应该确保我们总是只比较i
  • Magento 价格格式 - 2 或 3 位小数

    我需要将某些商品的价格设置为小数点后 3 位 我通过将 精度 变量更改为 3 来实现此目的 但这意味着网站上的每个价格都显示为小数点后 3 位 即 空购物车显示为 0 000 英镑 我只想在需要时显示小数点后第三位 但我不确定在代码库中哪里
  • 创建/追加节点与innerHTML

    有人有充分的理由使用其中一种而不是另一种吗 据我所知 创建 附加节点只是防止您创建无效代码 而innerHTML允许您一次注入多个节点 考虑到我需要插入几个标签 使用innerHTML似乎是有意义的 有人有不同的看法吗 这始终是一个有争议的
  • SaveMany 在 cakePHP 中同时更新多条记录不起作用

    我面临着使用 saveMany 同时更新多个记录的问题 我有如下关联 候选人hasMany候选人雇主 候选人雇主属于候选人 Candidate php 中的模型关联 public hasMany array CandidatesEmploy