php - CakePHP 多输入字段 w/saveAll() HABTM 多条记录插入不保存

标签 php mysql cakephp has-and-belongs-to-many

我在 StackOverflow 上找到了一些关于该主题的非常有用的教程和帖子,但我完全陷入了困境。

下面的所有内容都正常工作除了我的 HABTM Zip 数据

这是我的代码:

<?php for ($i = 1; $i <= 3; $i++) { // 3 fields at a time ?>

     <?php echo $this->Form->input('Plan.' . $i . '.plan_detail_id', array(
    'options' => $plans_list,
    'type'    => 'select',
    'empty'   => '-- Select a the Plan Detail --',
    'label'   => 'Select a the Plan Detail'
)); ?>

    <?php echo $this->Form->input('Plan.' . $i . '.monthly_cost', array('label' => 'Monthly Cost')); ?>
    <?php echo $this->Form->input('Plan.' . $i . '.dental_cost', array('label' => 'Dental Cost')); ?>
    <?php echo $this->Form->input('Plan.' . $i . '.age_id', array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?>
    <?php echo $this->Form->input('Plan.' . $i . '.applicant_id', array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type  --')); ?>
    <?php echo $this->Form->input('Plan.' . $i . '.state_id', array('label' => 'Select a State', 'empty' => '-- Select a State --')); ?>
    <?php echo $this->Form->input('"$i".Zip', array(
    'type'     => 'select',
    'multiple' => 'true',
    'label'    => 'Select the Zips'
));
<?php } // end for() ?>

我的 Controller 操作如下:

function bulk_add() {
    if (!empty($this->data)) {
        $this->Plan->create();
        if ($this->Plan->saveAll($this->data, array('validate' => 'false'))) {
            $this->Session->setFlash(__('The plan has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
        }
    }
    $ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
    $applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name ASC')));
    $states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));
    $zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));

    $this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));

    $plans_list = array();
    $plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
    foreach ($plans as $row) {
        $plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
    }
    $this->set('plans_list', $plans_list);
}

最佳答案

第三天:)

我无法让我的数组(具有多个条目)不被数字索引。并且多个表上的 saveAll() 需要一个键控数组才能正常工作。

我在下面有一个完整的数据转储,其中包含数字索引数组,并且不知何故它需要按键索引(我可以让它正常工作,但只能在单个记录插入时)..

我对bulk_add的看法

<?php for ($i = 1; $i <= 2; $i++) { ?>
<table>
  <tr>
  <td><?php echo $this->Form->input("$i.plan_detail_id", array(
    'options' => $plans_list,
    'type'    => 'select',
    'empty'   => '-- Select a the Plan Detail --',
    'label'   => 'Select a the Plan Detail'
));
?></td>
    <td><?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?></td>
    <td><?php echo $this->Form->input("$i.dental_cost", array('label' => 'Vision Cost')); ?></td>
    <td><?php echo $this->Form->input("$i.age_id", array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?></td>
    <td><?php echo $this->Form->input("$i.applicant_id", array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type  --')); ?></td>
    <td><?php echo $this->Form->input("$i.state_id", array('label' => 'Select a State', 'empty' => '--   Select a State --')); ?></td>
    <td>

    <?php echo $this->Form->input("$i.Zip", array('multiple' => 'true')); ?>
</td>
  </tr>
</table>
<?php } // end for() ?>
<?php
echo $this->Form->end(__('Submit', true));
?>

我的 Controller 代码:

function bulk_add() {
    if (!empty($this->data)) {
        $this->Plan->create();
        if ($this->Plan->saveAll($this->data, array('atomic' => 'false'))) {
            // Debug
            debug($this->data);
            $this->Session->setFlash(__('The plan has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
        }
    }
    $ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
    $applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name  ASC')));
    $states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));

$zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));
    $this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));

    $plans_list = array();
    $plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
    foreach ($plans as $row) {
        $plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
    }
    $this->set('plans_list', $plans_list);
}

这是调试转储:

Array
(
[Plan] => Array
    (
        [1] => Array
            (
                [plan_detail_id] => 36
                [monthly_cost] => 0
                [dental_cost] => 0
                [age_id] => 14
                [applicant_id] => 1
                [state_id] => 1
            )

        [2] => Array
            (
                [plan_detail_id] => 36
                [monthly_cost] => 0
                [dental_cost] => 0
                [age_id] => 2
                [applicant_id] => 4
                [state_id] => 1
            )

    )

[1] => Array
    (
        [1] => Array
            (
                [Zip] => Array
                    (
                        [0] => 487
                        [1] => 486
                        [2] => 485
                        [3] => 484
                        [4] => 483
                    )

            )

    )

[2] => Array
    (
        [2] => Array
            (
                [Zip] => Array
                    (
                        [0] => 485
                        [1] => 484
                        [2] => 483
                    )

            )

    )

)

关于php - CakePHP 多输入字段 w/saveAll() HABTM 多条记录插入不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4950487/

相关文章:

Php date() 在解析后给出了错误的时间

Cakephp 3 : Current password match in controller

php - CakePHP 将新模型关联添加到现有表

php - cakePHP 3.0 HABTM关系保存数据

javascript - 如何获取扩展图像文件 jquery.filer

php - paypal php sdk 设置定期付款

php - PHP 中的 undefined variable

MySql 错误 1064 使用 MySQL WorkBench 分配架构权限时

mysql - 如何使用 Spring Data JPA 和 MySQL 修复 Spring Boot 中的此错误

php - 我需要清理输入吗? PDO和mysql转义