• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Calc to database - Solved

PaulT

Member
I have a calc plugin element with this calculation:

$fields = array('geh_time_sheet_18_repeat___monday_total_hours','geh_time_sheet_11_repeat___tuesday_total_hours','geh_time_sheet_12_repeat___wednesday_total_hours','geh_time_sheet_13_repeat___thursday_total_hours','geh_time_sheet_14_repeat___friday_total_hours','geh_time_sheet_15_repeat___saturday_total_hours','geh_time_sheet_16_repeat___sunday_total_hours');
$total = 0;
foreach ($fields as $field)
{
$value = JArrayHelper::getValue($data, $field . '_raw', array());
$thisSum = array_sum($value);
$total += $thisSum;
}
return $total;

This shows the total hours for the week on a timesheet once the form is saved. I was wondering if there is a way get the form to send this result to the database after it has processed the calculation?
 
As usual, I'm confused.:confused:
Isn't the calc element already doing its thing and "sending the result to the database after it has processed the calculation"?
 
That is what I originally assumed but it isn't the case. The calculation processes on save and then displays the result but it doesn't get saved to the database, it merely recalculates the equation each time and displays it on screen.
This is per the reference in the Wiki about Calc Element:
IMPORTANT NOTE
The calc element should only be used to return a value. It should not be used to "do things", like update database tables. If you need to do "stuff" other than just return a value for the element, you should use a PHP form submission plugin.

The bit about using PHP form seems to indicate to me there is a away to get it to record into the database once it has calculated. That is what I am trying to find out.
 
I have plenty of calc elements in numerous tables - and they're all storing the calc value to the database. Some of them are pretty complex too. Though there are numerous quirks - and it sometimes seems like the calc element has a mind of its own.:eek: That Wiki notice is mostly to 'cover' situations (like yours apparently) where there can be issues.

Have you tried a different approach? Maybe something just doesn't 'play well' together. Instead of using JArrayHelper try just adding the '_raw' to the field names as you create the array. Then loop through the $data array using each of those element names.

If that still doesn't work, forget the calc element and just create a normal 'field' element - e.g. total_hours decimal(11,2). Then you should be able to do the same as your calc element by putting that code in a php form plugin - and the last thing that code does is update the 'total_hours' value before saving the data.

In either case, write some expected results to the php error log as you build the code and check that you're getting what you expect through each step of the process. ...And wishing you better luck.:)
 
I tried placing this in a Form Plugin PHP:
$fields = array('geh_time_sheet_18_repeat___monday_total_hours_raw','geh_time_sheet_11_repeat___tuesday_total_hours_raw','geh_time_sheet_12_repeat___wednesday_total_hours_raw','geh_time_sheet_13_repeat___thursday_total_hours_raw','geh_time_sheet_14_repeat___friday_total_hours_raw','geh_time_sheet_15_repeat___saturday_total_hours_raw','geh_time_sheet_16_repeat___sunday_total_hours_raw');
$total = 0;
foreach ($fields as $field)
{
$value = JArrayHelper::getValue($data, $field, array());
$thisSum = array_sum($value);
$total += $thisSum;
}

return $total;

But how do you tell it to save to the geh_time_sheet___total_hr_week element (This element has now been set as a decimal(13,2) field)?
 
I actually got this to work using this:

$fields = array('geh_time_sheet_18_repeat___monday_total_hours_raw','geh_time_sheet_11_repeat___tuesday_total_hours_raw','geh_time_sheet_12_repeat___wednesday_total_hours_raw','geh_time_sheet_13_repeat___thursday_total_hours_raw','geh_time_sheet_14_repeat___friday_total_hours_raw','geh_time_sheet_15_repeat___saturday_total_hours_raw','geh_time_sheet_16_repeat___sunday_total_hours_raw');
$total = 0;
foreach ($fields as $field)
{
$value = JArrayHelper::getValue($data, $field, array());
$thisSum = array_sum($value);
$total += $thisSum;
}

$formModel->updateFormData('geh_time_sheet___total_hr_week',$total);

With Process script set to: Start of form submission (onBeforeProcess)

:)
Thanks a lot Bauer for your help!
 
Sorry, I slept in this morning after being up late last night.
But I was just about to respond to the email notice of your prior reply - informing you that you don't "return" anything when using the PHP plugin. Good to know you figured it all out. :D

And thanks, startpoint, for including the Wiki link.:)
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top