开发者

CakePHP Save for each checked item of separate model

I'm having some trouble saving multiple entries in CakePHP. I'm not sure whether saveAll() will do the trick as I'm trying to save an entry for each item checked from a join table which joins two separate tables to the one being saved to.


I have the following tables: Samples, Results and a join table called SampleTypeTests (joins two separate tables - SampleTypes & Tests).

Each Sample has an FK sample_type_id

Each Result has the FK sample_id, sample_type_test_id

I have an Add view for Results which lists all SampleTypeTests for a Samples.sample_type_id

My Add view form looks like this at the moment:

<?php 
echo $this->Form->create('Result');
echo '<fieldset>';

    echo '<legend>Choose Analysis</legend>';

    echo $this->Form->input('sample_id', array('type'=>'hidden', 'value' => $this->passedArgs['0']));

    echo $this->Form->input('sample_type_test_id',array(
        'label' => __('Tests',true),
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $sampleTypeTests,
        'selected' => $html->value('Result.sample_type_test_id'),
        'hiddenField' => false
    )); 

echo '</fieldset>';

echo $this->Form->开发者_C百科end(__('Submit', true)); ?>

The saving part is where I'm having the biggest headache. I need a Results entry created for each SampleTypeTests checked and that SampleTypeTest.id to be added to the FK Results.sample_type_tests_id.

I have tried to adapt the following to my needs http://mrphp.com.au/code/working-habtm-form-data-cakephp#habtm_checkbox

Here is what I got:

function add() {

    if ($this->Result->saveAll($this->data['Result'])) {
        $this->Session->setFlash(__('All required tests have been saved', true));
        $this->redirect(array('action' => 'index'));
    } else {
        $this->Session->setFlash(__('Could not be saved. Please, try again.', true));
    }

// Everything else
} 

This hasn't worked. The above may be wrong. It is more to demonstrate what I've attempted. I'm getting very lost in it all and I need some help. Do I need to use saveAll? Do I need to nest the save? Have I made it far more complicated than it needs to be?


UPDATE:

I have noticed the above form outputs the array as

Array (
[Result] => Array
    (
        [sample_id] => 21
        [sample_type_test_id] => Array
            (
                [0] => 1
                [1] => 24
                [2] => 16
                [3] => 71
            )

    )
)

When what it should produce is:

Array
(
[Result] => Array(
        [0] => Array
            (
                    [sample_id] => 21
                    [sample_type_test_id] => 1
                )
        [1] => Array
            (
                    [sample_id] => 21
                    [sample_type_test_id] => 24
                )
        )
)

Then I can simply use saveAll($this->data['Result'])

Any hints on how I can improve the form to output the desired array?


I just gave a similar answer to another post that might be useful to you, I don't have time to modify it right now, but let me know if this doesn't answer your question then I will elaborate.

How to create a view page to save two objects in cakePHP

ok... after reading your problem further, I think this should do the trick...

foreach ($this->data['Result']['sample_type_test_id'] as $v) {
$data = array(
    'id'=>'',
    'sample_id' => $this->data['Result']['sample_id'],
    'sample_type_test_id' => $v
    );
$this->SampleTypeTest->save($data);
}

This should save to the db as :

id | sample_id | sample_type_test_id
1    21          1
2    21          24
3    21          16
4    21          71

Ok try this then...

        $data;
    $i=0;
    foreach ($this->data['Result']['sample_type_test_id'] as $v) {
        $data[$i] = array(
            'id'=>'',
            'sample_id' => $this->data['Result']['sample_id'],
            'sample_type_test_id' => $v
            );
        $i++;
    }
    $this->Result->save($data);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜