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

  • Home
  • SEARCH
  • 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 4006214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:29:16+00:00 2026-05-20T08:29:16+00:00

I have been able to use CakePHP’s saveAll method to simultaneously create ‘Members’ and

  • 0

I have been able to use CakePHP’s saveAll method to simultaneously create ‘Members’ and enroll them in an ‘Event’ (creating the HABTM link record), which is awesome. For example, this code creates two new ‘Members’ and adds a record for each of them to the ‘EventsMember’ table, enrolling them ‘Event’ 10:

$data = array(
  '0' => array(
    'Member' => array('email' => 'nobody@nowhere.com'),
    'Event' => array('id' => 10)
  ),
  '1' => array(
    'Member' => array('email' => 'somebody@nowhere.com'),
    'Event' => array('id' => 10)
  )
);
$this->Member->saveAll($data);

However, the record in the ‘EventsMember’ table also has a field called ‘role’ that holds something like “Presenter” or “Host” or “Attendee” and I would like to save that data when I create the relationship. I tried this and it does not work (the ‘EventsMember’ ‘role’ field is always blank):

$data = array(
  '0' => array(
    'Member' => array('email' => 'nobody@nowhere.com'),
    'Event' => array('id' => 10),
    'EventsMember' => array('role' => 'Host')
  ),
  '1' => array(
    'Member' => array('email' => 'somebody@nowhere.com'),
    'Event' => array('id' => 10),
    'EventsMember' => array('role' => 'Attendee')
  )
);
$this->Member->saveAll($data);

I’m wondering if this is even possible, and if maybe I have to use some kind of callback like beforeSave or afterSave to get this done? I’ve read that there are some problems with these callbacks when using saveAll, so I’m looking for any tips on what would be the best practice here.

Thanks!

EDIT: I took Adam’s advice and made the following changes to my models:

// Event.php
var $hasMany = array('EventsMember');

// Member.php
var $hasMany = array('EventsMember');

// EventsMember.php
var $belongsTo = array('Event', 'Member');

Then in my controller, my code looked almost identical to my second example, except I called the saveAll() method from the EventsMember model, as described in the documentation:

$data = array(
  '0' => array(
    'Member' => array('email' => 'nobody@nowhere.com'),
    'Event' => array('id' => 10),
    'EventsMember' => array('role' => 'Host')
  ),
  '1' => array(
    'Member' => array('email' => 'somebody@nowhere.com'),
    'Event' => array('id' => 10),
    'EventsMember' => array('role' => 'Attendee')
  )
);
$this->EventsMember->saveAll($data);

The result was no Member or EventsMember records were saved at all. I tried triggering the save from the Member model ($this->Member->saveAll($data);) and this saved the Member records, but not the joining EventsMember records.

I tried removing the pre-existing HABTM associations, and it made no difference. The beforeSave method of the EventsMember model is getting triggered when I call $this->EventsMember->saveAll($data); but it looks like it won’t actually save anything.

I’m stymied.

UPDATE: It turns out that no records were created because the joining records were all being created with Event ids and Member ids of 0, which goes against a unique key I have on those two fields combined (that is, no Member can enroll in an Event twice).

Does this suggest that the join model saveAll functionality is not working as documented, since the Member record isn’t getting created (meaning there is no Member id to use in the joining record), and the existing Event id is not being passed to the joining EventsMember record either?

VERDICT: I changed the controller to loop on each record and attempt to $this->EventsMember->saveAll($data) for each index of the array, instead of passing the entire array at once. It worked, but was significantly slower than my first example (at the very top). Mind you, I am using transactions, so perhaps using the atomic => false; option would speed things up, while still allowing me to recover from any invalid records.

Bottom line, if you need to save extra data in join table records, you have to process them one at a time. If not, use the method at the top for the best performance.

  • 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-20T08:29:17+00:00Added an answer on May 20, 2026 at 8:29 am

    You can use The Join Model in this case.

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

Sidebar

Related Questions

I am trying to use the following code, which I have not been able
I have been able to create ASPX pages without the code behind, but I
I have been able to use logman with some success to dump some kernel
I have been able to use Jquery with ASP.NET because I know where to
I have been able to zip the contents of my folder. But I would
I have been able to copy the raw data from an otherwise inaccessible USB
The best that I have been able to come up with is: strlen(preg_replace('/^([\\*]*)\s(.+)/',$1,$line)); ^^That
It seems obvious that some people have been able to figure out how to
I have not been able to find a way to cancel/terminate asynchronous read operation
The project is ASP.NET 2.0, I have never been able to reproduce this myself,

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.