Forums

Save Data to Two Tables

kkurtz 27 Nov, 2019
I am trying to save data to two tables which need to be related. I have read and tried the suggestions from an earlier post (Save form data into 2 tables, posted August 17, 2019 ) many times. But I guess I am missing something.
I am able to save data to the first table, which is the Joomla users table. I want to then use the id from that table and save it to another table so I can relate the two tables.
So I save the user data from a form. The model name for the user file is Users.
Then I read the user file as follows:
Name is read_users_id
Model is Users
Select type is First Matching record
Order is id, descending
Fields to retrieve is Users.id
According to the debug, the Users.id is retrieved successfully.

Then to save to the second table:
Name: read_info
Database table: newuserinfo
Write Action: Insert
Data Provider: {var:read_users_id#.Users.id}
Model: Users
I have tried using Data Override using the name of the field in the second table and specifying what I want to to be set to
Set userfile_id to {var:read_data#.Users.id} on insert
I have also tried to set a condition: userfile_id = Users.id
Apparently I am not using the correct syntax for the data I want to pass because am not getting a value for userfile_id.

Also, the primary key of the receiving table is aid and it is not being auto incremented.

So two issues, how can I get the correct value in the userfile_id field of the receiving table ?
And how can I cause the aid to be auto incremented?

Thanks for your help.
Karen
healyhatman 28 Nov, 2019
When you save into the first table, part of the data returned will include the created ID. No need to read the database looking for the newly created object, ESPECIALLY since the way you're doing it will run into problems if a new entry is saved to the database in between your save and read.

So it will be available as {var:save_data_action_name.ModelName.id_column_name}

Should probably also be using the Save User action, instead of saving directly to the table.

If the aid column in your other table is not being auto incremented, make sure you actually have it set to auto increment. Do that in PHPMyAdmin, or in Chronoform's table editor. But seriously just do it in PHPMyAdmin.
healyhatman 28 Nov, 2019
The Data Source field takes an array of data, and matches up attributes in the array with the same named column. So that's not much use to you.

You say you're using {var:read_data#.Users.id} as the data override - can I ask, are you typing LITERALLY that?
kkurtz 03 Dec, 2019
Answer
HealyHatman,

Just FYI...I didn't get a notification that you had posted a reply. Is that supposed to be automatic? If so, it isn't working.

First, I was able to get the aid field to auto increment by using phpmyadmin.

I was able to save the second file with the id from the User file.

Thanks for your help.
This topic is locked and no more replies can be posted.