0

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

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);
}
OldWest
  • 2,355
  • 7
  • 41
  • 61

3 Answers3

1

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
                    )

            )

    )

)

OldWest
  • 2,355
  • 7
  • 41
  • 61
0

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

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

OldWest
  • 2,355
  • 7
  • 41
  • 61
dogmatic69
  • 7,574
  • 4
  • 31
  • 49
  • What you proposed has been tried (I've tried about everything) and does not work. Sounds like this is a new issue to you as well. I am sorry the email bothered you : ) Normally I am in close relation with my programming peers and we tend to help one another when in need. Just what I am use to. So sorry about that : ) – OldWest Feb 10 '11 at 15:03
0

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

Mark Story
  • 1,269
  • 6
  • 12
  • Thanks, Mark. The odd thing is all data is saving currently as I have it with the exception of Zip. I will try your above mentioned pattern to see if that changes anything. I also had another recommendation on how to approach this, but it did not seem too Cakish. – OldWest Feb 11 '11 at 04:05