【问题标题】:CakePHP Multiple Entry Fields w/ saveAll() HABTM multiple records Insert not savingCakePHP 多输入字段 w/ saveAll() HABTM 多条记录 插入不保存
【发布时间】:2011-02-09 21:12:47
【问题描述】:

我在 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() ?>

我的控制器动作如下:

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);
}

【问题讨论】:

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


    【解决方案1】:

    第 3 天:)

    我无法让我的数组(包含多个条目)不被数字索引。多个表上的 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));
    ?>
    

    我的控制器代码:

    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
                        )
    
                )
    
        )
    

    )

    【讨论】:

      【解决方案2】:

      如果您想保存很多行,您的输入名称似乎是错误的。您可能想要“$i.Plan.field”和“$i.Zip”

      【讨论】:

      • 谢谢,马克。奇怪的是,除了 Zip 之外,所有数据目前都在保存,因为我拥有它。我会尝试你上面提到的模式,看看是否会改变任何东西。我还有另一个关于如何解决这个问题的建议,但它似乎不太 Cakish。
      【解决方案3】:

      $this->Form->input('Zip');它在书中

      为你查找 find('list') 很好的 foreach hack。

      【讨论】:

      • 您提出的建议已经尝试过(我已经尝试了所有方法)并且不起作用。听起来这对您来说也是一个新问题。很抱歉,这封电子邮件打扰了您:) 通常我与我的编程同行关系密切,我们倾向于在需要时互相帮助。正是我习惯的。很抱歉:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多