I am able to save data to a table with the DB Save Action, and am trying to follow the instructions from this 2014 post. The primary response in the post that I am referencing is:
In the second database save, I added the following to the Data Override on Insert
I have placed the custom code action between the two Database Save actions, but the debugger shows that tid is blank, and that the insert is saving a null value to the second table.
The post I am using is from CF5. Is there a different approach in CF6, or have I made a mistake?
Thanks!
Bill
The value of the inserted record primary key will be added to the data array after the db save, which in your case I believe is 1, then you can use a new db save, but you should copy the value to a new field matching the foreign key's name in the 2nd table, and if the 2nd table's pkey is also "id" then you must unset the id value in the data array: <?php $form->data["2nd_table_fkey"] = $form->data["id"]; //assuming pkey in 1st table is "id" unset($form->data["id"]); //assuming pkey in 2nd table is also "id", and so we need to remove this value to avoid an update The code above should be inside a "custom code" action before the 2nd db save. Regards, MaxIn my case, the line is
<?php $form->data[fk_tid] = $form->data[tid]; ?>tid is the primary key of table 1, and fk_tid is the foreign key in table 2. In the debugger,
In the second database save, I added the following to the Data Override on Insert
tid:{data:fk_tid}I see in the debugger that tid in the primary table is being created, as expected.
I have placed the custom code action between the two Database Save actions, but the debugger shows that tid is blank, and that the insert is saving a null value to the second table.
[save_data8] => Array ( [data] => Array ( [email] => a@bc.com [created] => 2018-01-31 01:39:56 [user_id] => 204 [tid] => )I have also tried placing the custom code action in the Success block of the first action, and at the top of the body of the second action, but the result is the same.
The post I am using is from CF5. Is there a different approach in CF6, or have I made a mistake?
Thanks!
Bill