I have an issue with needing to save some specific data on user creation and unsure as to how to approach it.
I have three models, User, Portfolio, and Account. User hasOne Portfolio, Portfolio hasMany Account. I'm trying to figure out how to save Account data when a User is being created. But this isn't where it ends.
Currently on User creation, a Portfolio is also created after the user is successfully created. Using these 3 lines in the UsersController.php file:
if ($this->User->save($this->request->data['User'])) {
$this->request->data['Portfolio']['user_id'] = $this->User->id;
if ($this->User->Portfolio->save($this->request->data['Portfolio'])) {
The accounts table is fairly meager, id, portfolio_id, currency, and amount. On user creation, multiple rows need to be added to the accounts table, one for each currency we support (at the moment 2, will likely be more later), and each row could have different amounts.
My problems are two-fold, first, how do I create this new data in the user registration form? And second, what do I do with it once a user is successfully created?
I'll admit I haven't done much research on this as I don't even really know what to search for that would find this particular problem. If you need anymore info please just ask and I'll edit this post to supply it.
EDIT: After following advice to use saveAll to save all the data and associated data, I've run into another issue. My portfolio table is the very definition of slim at the moment. Right now, it only holds a user_id and that's it. So when I run the save operation, there is no data in the request->data array to signify a Portfolio needs to be saved, as the only data that needs to be saved comes from the user_id which can only be obtained after a user is created.