Datatables Editor PHP SDK: Set default-value on create or when certain field is empty
Datatables Editor PHP SDK: Set default-value on create or when certain field is empty
mattomedia
Posts: 8Questions: 1Answers: 0
Hi Guys,
I am struggling to correctly use the Validator and Formatter-Function of the Datatables-Editor PHP SDK.
Use Case:
Generate a new UUID on create of a new Entry or generate a new UUID when the Field uuid
is empty.
Shouldn't this code lead to creating a new UUID via rex_yform_value_uuid::guidv4()
when the field uuid
is empty?
Field::inst( $mainTable.'.uuid' )
->setFormatter(
Format::ifEmpty( rex_yform_value_uuid::guidv4() )
),
Replies
Is
rex_yform_value_uuid::guidv4()
a PHP function? If so, then yes, what you have should result in a UUID being inserted if the submitted value is an empty string.If that isn't happening, add the
->debug(true)
method just before->process(...)
and then can you show me the SQL that is being generated (shown in the JSON response) and also the data being submitted please?Thanks,
Allan
HI Allan,
thank you for your answer.
This is the PHP-Function for the UUID:
https://github.com/yakamara/redaxo_yform/blob/49cdff49a7323dc2d963a9640286d75fa5bac5a8/lib/yform/value/uuid.php#L25-L36
Error:
Query:
Bindings:
I'd be tempted to use this rather than
ifEmpty
. Either should work, but the benefit of this method is that the UUID will only be generated when it is needed. WithifEmpty
it would be generated every single time that script runs.However, as I said, either method should work, so I'm not certain that this will fix the issue. Can you show me your full PHP for Editor?
Allan
Thank you for your effort.
The given code results in the same error:
An SQL error occurred: SQLSTATE[HY000]: General error: 1364 Field 'uuid' doesn't have a default value
This is the PHP-Code for Editor:
It has just clicked for me that
uuid
isn't in the submitted data. ThesetFormatter
won't run for that, since nothing was submitted. It also can't tell if there is currently a value or not, again because it wasn't submitted.Presumably you don't want the end user to be able to change it?
I think what your best option is here, is to use a database trigger. On insert or update, check if it has a value and if not, generate a new uuid value.
If that isn't an option, what you would need to do in this case is use server-side events and on create (
preCreate
) do:On edit (
preEdit
) you would need to make a database query to see if it has a value or not. If not, then generate a value and usesetValue
again.This assumes that you might have database entries already which don't have a uuid value. If you either don't have that issue, or there aren't yet any database rows for this table, then you could just use the
preCreate
event to set the UUID value when a new row is created.Allan
Great!
I was assuming, that every field in the Editor-Class is always regarded. No matter if data is sent or not.
With the preCreate-Action I am able to generate a new UUID for every new entry. That UUID will never change again. Exactly what I needed.
Thanks so much for the support. Appreciate it!
Keep on doing a great job on datatables and the forum support. Pretty impressive. Thanks!
Thank you
Great to hear that did the job for you.
Allan
On a sidenote.
In the manual for server-side-events is a dead link for the example.
The Description of the example-code for logging changes mentions for
Line 2 the Database->insert-Method
, which is linked to a 404, but propably moved to here:https://editor.datatables.net/docs/2.1.0/php/classes/DataTables-Database.html#method_insert
Thanks for pointing that out! I updated to a different documentation generator and missed that link. Fixed locally and I'll deploy the change soon.
Allan