开发者

CakePHP Multiple Entry Fields w/ saveAll() HABTM multiple records Insert not saving

开发者 https://www.devze.com 2023-02-10 02:38 出处:网络
I\'ve found some very helpful tutorials and posts on StackOverflow about this topic, but I am completely stuck on one point.

I've found some very helpful tutorials and posts on StackOverflow about this topic, but I am completely stuck on one point.

Everything below IS working with the exception of my HABTM Zip data.

Here is my code:

<?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->For开发者_运维知识库m->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() ?>

My controller action is as follows:

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


Day 3 : )

I cannot get my array (with multiple entries) to NOT be indexed numerically. And a keyed array is required for the saveAll() on multiple tables for it to work properly.

I have a complete data dump below with the numeric indexed array, and somehow it needs to be indexed by keys (I can get it to work correctly but ONLY on single record insert)..

My view for 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));
?>

My controller code:

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

Here is the debug dump:

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
                    )

            )

    )

)


Looks like you built your input names wrong if you want to save many rows. You probably want "$i.Plan.field", and "$i.Zip"


$this->Form->input('Zip'); its in the book

look up find('list') for you nice foreach hack.

0

精彩评论

暂无评论...
验证码 换一张
取 消