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 8750631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:54:03+00:00 2026-06-13T12:54:03+00:00

I was hoping to create and save the associated data like what I did

  • 0

I was hoping to create and save the associated data like what I did with belongsTo data – the associated data are created on the fly and foreign ID is also generated and saved on the fly by a single call to saveAll or saveAssocated which has transaction built-in.

But this seems not the case for data in hasMany relationship. Take User and Comment as example. Comment has user_id as the foreign key.

It seems that I cannot save User $data by using single saveAll($data) on User

Array(
    'name' => 'Jack',
    'email' => 'jack@abc.com',
    'Comment' => array(
         array(
            'title' => 'I like this article.'
         )
    )
)

I read some docs. It seems that I need to mention the user_id as the foreign key for the Comment data to create correctly.

If that’s the case, since I don’t have user ID until it’s created, it seems that I need to code to let SAVE happen twice.

I really think that I am missing something. There should be a CAKE way for doing this.

  • 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-06-13T12:54:04+00:00Added an answer on June 13, 2026 at 12:54 pm

    This is done automatically by Cake as long as you follow the conventions and format the data accordingly. For hasMany associations, the main model data, and the associated model data, need to be set on string keys on the same level, like

    array
    (
        'User' => array(),
        'Comment' => array()
    )
    

    Also note that

    The saveAll function is just a wrapper around the saveMany and
    saveAssociated methods. it will inspect the data and determine what
    type of save it should perform. If data is formatted in a numerical
    indexed array, saveMany will be called, otherwise saveAssociated is
    used.

    This function receives the same options as the former two, and is
    generally a backwards compatible function. It is recommended using
    either saveMany or saveAssociated depending on the case.

    So either will do.

    Long story short, the model data needs to be separated and indexed by string keys, it’s essentially the same format as a find() call would return it. That way Cake will know that it needs to save associated data, and will automatically insert the foreign key for the Comment records.

    array
    (
        'User' => array
        (
            'name' => 'Jack',
            'email' => 'jack@abc.com'
        ),
        'Comment' => array
        (
            array
            (
                'title' => 'I like this article.'
            )
        )
    )
    

    See also

    • Cookook > Models > Saving Your Data > Associations: Linking Models Together > hasMany

    • Cookook > Models > Saving Your Data > Model::saveMany()

    • Cookook > Models > Saving Your Data > Model::saveAssociated()

    • Cookook > Models > Saving Your Data > Model::saveAll()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I looking to create a custom calender with Zend Framework, I am hoping that
What's the right way in Drupal to create reports? I was hoping to use
I'm hoping someone can help on this one, I have created a custom object
Hoping for a quick answer here. OK, since I do a lot of single
The contents of one TabItem is large CPU expensive to create. Would like to
I trying to create a extension for a browser and i want do save
I am hoping to create an ajax sign in form, which redirects the user
I'm creating an air application that allows the user to create, edit and save
We have the requirement to take a form submission and save some data, then
When a save , or create is tossed towards the server, the server responds

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.