Updating a column which contains no value on edit
Updating a column which contains no value on edit
Ok, this is a slightly tricky one to explain, so my apologies if it's not very clear.
I have a table which contains booking information for a holiday let, and among the values is a total cost column.
At the moment, the column is empty for all records. It will be populated when the booking form is submitted in future, but I need to be able to update it for existing items which have no value in them.
I'm using the PHP back end, and I have a full licence for Editor.
When I construct the Editor, I'm loading the fields, which includes field value for the number of guests, number of nights etc - everything needed to populate the total cost.
The cost field is loaded as shown:
Field::inst( 'total_cost' )
->validator( Validate::numeric() ),
And I have a function in another PHP file which calculates the total cost, and called as follows:
function getBookingCost ($numberAdults, $numberChildren, $numberYoungChildren, $numberDogs, $numberNights) {
}
Is there a way that I can modify the field definition so that if there is no cost associated with the column, it would call my function to calculate the cost, then update the column in the table?
If not, I can put a background task together to make this work, however it would be useful to have this populated dynamically when the table loads rather than rely on a cron task.
I'd really appreciate any help with this, as I can't find anything in the documentation which would lead me in the right direction.
Many thanks
This question has an accepted answers - jump to answer
Answers
Sure you would be using a getFormatter and you can also use a setFormatter if you want to make sure the field is always filled (just in case the user can manipulate the field at the front end).
If those fields that you pass to your function are also included in your Editor instance you can refer to them as $data["numberAdults"] etc. Instead of using $val you could also use $data["total_cost"].
You can even have fully synthetic fields in Editor that don't even exist in your database.
Here is an example. Editor returns a field called "is_manual_addtion" which is generated from a combination of other Editor fields.
@rf1234 Thank you!! That is exactly what I needed!!
Once again, DataTables, Editor, and the folks in the community have proven to be the absolute best experience I have ever had working with any library in any language. Ever.
I had to change some things in my code and in the DB to resolve some formatting issues for the date ranges, but your approach works beautifully!