Create Multiple (slightly different) Records on Submit

cshort

Fabrik lover!
Hi everyone, i'm really hoping someone can help me out with this issue, it's driving me nuts!

Using the form php plugin i'm trying to submit and clone a record based on how many repeatable items are added by a user.

For example, when a user submits the form...

My form has a 'feature date' group. The user has added three 'feature dates' which means I need to create three new records. The first record needs to contain the first repeatable 'feature date' value in a non-repeatable date field named 'feature_date', the second record needs to contain the second repeatable 'feature date' value in a non-repeatable date field named 'feature_date', and so on!

In case the above description is confusing, this is how I imagine the php code might look:
PHP:
// get the array of repeatable feature dates
$dates = $formModel->formData["mpm_fabrik_deals_8_repeat___feature_date_raw"];
// for each repeatable feature date
foreach ($dates as $date)
{
$formModel->formData('mpm_fabrik_deals___feature_date', $date, true);
$formModel->processToDB();
}

So I know how to modify the array of form data, I just don't know how to store each array as a new record?

Thanks in advance for any help
 
I have an update on my code: It creates one new record per feature date :) BUT each new record is missing different data :(

PS i'm using 'onBeforeProcess' as the 'process script' in the php form plugin
PHP:
$dates = $formModel->formData["mpm_fabrik_deals_8_repeat___feature_date_raw"];
 
$count = 0;
foreach ($dates as $date) {
  if($count == 0) {
    $formModel->updateFormData('mpm_fabrik_deals___feature_date', $date[0], true);
    $formModel->processToDB();
    $count++;
    continue;
  }
$formModel->updateFormData('mpm_fabrik_deals___feature_date', $date, true);
$formModel->processToDB();
}
 
Hi cshort,

I'd love to help you with this but I'm not sure from your post what help you're asking for specifically or which bit of the code you are stuck on.

Have you read this info about how to create new records - http://fabrikar.com/forums/index.php?wiki/common-php-tasks/#insert

Let me know, sure we can help find you a solution!

Hannah

Hi Hannah, thanks for your reply. I would love you to help me too! What i'm trying to do is interrupt the normal Fabrik submit function by manipulating the array of data (multiple times, depending on how many 'feature dates' the user adds to the form group) and then save each data array as a new record.

Charlie
 
Please know I'm not an expert but I do know the value of someone making suggestions when you are banging you head against the wall! :eek: I have been there myself a few times already this week!

I have an update on my code: It creates one new record per feature date :) BUT each new record is missing different data :(

So what does your data structure look like? Can you do a var_dump of $dates before the foreach loop and we can have a look?

PHP:
    $formModel->updateFormData('mpm_fabrik_deals___feature_date', $date[0], true);

I'm assuming that it is this line in your foreach loop that you think should be creating each new record with the different data? var_dump will help us see if you are accessing the array correctly...

Ok, hope that helps, let's see what you get!
 
Awesome thanks Hannah! This is a full dump of the form data:

Code:
array(74) { ["mpm_fabrik_deals___id"]=> string(1) "2" ["mpm_fabrik_deals___date_time"]=> string(19) "2015-04-15 08:55:01" ["mpm_fabrik_deals___account_id-auto-complete"]=> string(34) "001C0000018eI3o - The Little Snail" ["mpm_fabrik_deals___account_id"]=> array(1) { [0]=> string(15) "001C0000018eI3o" } ["mpm_fabrik_deals___opportunity_name"]=> string(4) "test" ["mpm_fabrik_deals___owner"]=> array(1) { [0]=> string(3) "565" } ["mpm_fabrik_deals___country"]=> array(1) { [0]=> string(2) "au" } ["mpm_fabrik_deals___division"]=> array(1) { [0]=> string(1) "3" } ["mpm_fabrik_deals___record_type"]=> array(1) { [0]=> string(2) "g1" } ["mpm_fabrik_deals___gb"]=> int(3000) ["mpm_fabrik_deals___gr"]=> float(300) ["mpm_fabrik_deals___merchant_email"]=> string(28) "cshort@sample.com.au" ["mpm_fabrik_deals_3_repeat___id"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals_3_repeat___option_name"]=> array(1) { [0]=> string(3) "One" } ["mpm_fabrik_deals_3_repeat___parent_id"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals_3_repeat___initial_cap"]=> array(1) { [0]=> string(3) "100" } ["mpm_fabrik_deals_3_repeat___original_price"]=> array(1) { [0]=> string(2) "40" } ["mpm_fabrik_deals_3_repeat___sample_price"]=> array(1) { [0]=> string(2) "30" } ["mpm_fabrik_deals_3_repeat___margin"]=> array(1) { [0]=> string(2) "10" } ["mpm_fabrik_deals___planner"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals___feature_date"]=> string(19) "2015-04-22 00:00:00" ["mpm_fabrik_deals_8_repeat___id"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" } ["mpm_fabrik_deals_8_repeat___feature_date"]=> array(3) { [0]=> string(19) "2015-04-15 15:30:13" [1]=> string(19) "2015-04-22 15:30:43" [2]=> string(19) "2015-05-06 15:30:47" } ["mpm_fabrik_deals_8_repeat___parent_id"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" } ["listid"]=> string(1) "1" ["listref"]=> string(1) "1" ["rowid"]=> string(1) "2" ["Itemid"]=> string(3) "112" ["option"]=> string(10) "com_fabrik" ["task"]=> string(12) "form.process" ["isMambot"]=> string(0) "" ["formid"]=> string(1) "1" ["returntoform"]=> string(1) "0" ["fabrik_referrer"]=> string(58) "http://tools.sampleau.com.au/internal/media-plan-manager/" ["fabrik_ajax"]=> string(1) "0" ["package"]=> string(6) "fabrik" ["packageId"]=> string(1) "0" ["73e3fe9e6d5fa5d00f710af8babf9437"]=> string(1) "1" ["format"]=> string(4) "html" ["fabrik_repeat_group"]=> array(4) { [1]=> string(1) "1" [3]=> string(1) "1" [5]=> string(1) "1" [8]=> string(1) "3" } ["fabrik_group_rowids"]=> array(2) { [3]=> string(4) "[""]" [8]=> string(4) "[""]" } ["Submit"]=> string(0) "" ["mpm_fabrik_deals___id_raw"]=> string(1) "2" ["mpm_fabrik_deals___date_time_raw"]=> string(19) "2015-04-15 08:55:01" ["mpm_fabrik_deals___account_id_raw"]=> array(1) { [0]=> string(15) "001C0000018eI3o" } ["mpm_fabrik_deals___opportunity_name_raw"]=> string(4) "test" ["mpm_fabrik_deals___owner_raw"]=> array(1) { [0]=> string(3) "565" } ["mpm_fabrik_deals___country_raw"]=> array(1) { [0]=> string(2) "au" } ["mpm_fabrik_deals___division_raw"]=> array(1) { [0]=> string(1) "3" } ["mpm_fabrik_deals___record_type_raw"]=> array(1) { [0]=> string(2) "g1" } ["mpm_fabrik_deals___gb_raw"]=> int(3000) ["mpm_fabrik_deals___gr_raw"]=> float(300) ["mpm_fabrik_deals___merchant_email_raw"]=> string(28) "cshort@sample.com.au" ["mpm_fabrik_deals_3_repeat___id_raw"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals_3_repeat___option_name_raw"]=> array(1) { [0]=> string(3) "One" } ["mpm_fabrik_deals_3_repeat___parent_id_raw"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals_3_repeat___initial_cap_raw"]=> array(1) { [0]=> string(3) "100" } ["mpm_fabrik_deals_3_repeat___original_price_raw"]=> array(1) { [0]=> string(2) "40" } ["mpm_fabrik_deals_3_repeat___sample_price_raw"]=> array(1) { [0]=> string(2) "30" } ["mpm_fabrik_deals_3_repeat___margin_raw"]=> array(1) { [0]=> string(2) "10" } ["mpm_fabrik_deals_3_repeat___gr_raw"]=> float(300) ["mpm_fabrik_deals_3_repeat___gb_raw"]=> int(3000) ["mpm_fabrik_deals___planner_raw"]=> array(1) { [0]=> string(0) "" } ["mpm_fabrik_deals___feature_week_raw"]=> string(7) "17-2015" ["mpm_fabrik_deals___feature_date_raw"]=> string(19) "2015-04-22 00:00:00" ["mpm_fabrik_deals_8_repeat___id_raw"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" } ["mpm_fabrik_deals_8_repeat___feature_date_raw"]=> array(3) { [0]=> string(19) "2015-04-15 15:30:13" [1]=> string(19) "2015-04-22 15:30:43" [2]=> string(19) "2015-05-06 15:30:47" } ["mpm_fabrik_deals_8_repeat___parent_id_raw"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" } ["__pk_val"]=> string(1) "2" ["view"]=> string(4) "form" ["fabrik_show_in_list"]=> array(0) { } ["mpm_fabrik_deals_3_repeat___gr"]=> array(1) { [0]=> float(300) } ["mpm_fabrik_deals_3_repeat___gb"]=> array(1) { [0]=> int(3000) } ["mpm_fabrik_deals___feature_week"]=> string(7) "17-2015" }

and this is a dump of $dates:

Code:
array(3) { [0]=> string(19) "2015-04-15 00:00:00" [1]=> string(19) "2015-04-22 00:00:00" [2]=> string(19) "2015-05-06 00:00:00" }
 
Code:
array(3) { [0]=> string(19) "2015-04-15 00:00:00" [1]=> string(19) "2015-04-22 00:00:00" [2]=> string(19) "2015-05-06 00:00:00" }

Ok, so I think you want each of these dates as your separate records? Does the following do what you want?

Code:
$count = 0;
foreach ($dates as $date) {
  if($count == 0) {
    $formModel->updateFormData('mpm_fabrik_deals___feature_date', $date[$count], true);
    $formModel->processToDB();
    $count++;
    continue;
  }

So basically instead of calling $date[0] you substitute in $count for the 0. $count is incrementing on each loop value so will go through each value from the $dates array.

Any good?
 
Hi Hannah, I really appreciate your help! There's no issue in creating the different arrays of data with the new dates, I can do that no problem. What I can't figure out how to do is save each new array as a new record. The problems start at this line: $formModel->processToDB();.
 
Hi cshort,
Have you read this info about how to create new records - http://fabrikar.com/forums/index.php?wiki/common-php-tasks/#insert

And this info on creating new records doesn't help? Isn't $formModel->updateFormData() all about updating existing records, and you are trying to create new ones? Don't you need to insert some new records into the database rather than trying to update. Seems like the wrong action.

This post might help give you another example on inserting/creating new records...

http://fabrikar.com/forums/index.ph...e-a-form-s-field-with-the-records-primary-key

H
 
Hi Hannah, I was hoping to not have to reinvent the wheel. Since the function to save the form data to the database is already there, I just want to initiate it several times with the manipulated arrays of form data. Ill keep trying and let you know if I find a solution
 
Well good luck, Charlie - I'm sorry I couldn't be of more help. :( I see how you are trying to do things, and get the simplicity of it. You've always got the means of inserting records in a more roundabout fashion as a backup I suppose! ;)

I'd love to hear what your solution is when you get it working how you want - please let me know as I've been playing about this with and am really curious to know the answer.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top