Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4064602
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:52:35+00:00 2026-05-20T15:52:35+00:00

I’m having some trouble saving multiple entries in CakePHP. I’m not sure whether saveAll()

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:52:35+00:00Added an answer on May 20, 2026 at 3:52 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.