Copie Data with after_insert_funktion
Copie Data with after_insert_funktion
Hello,
is there a possibility when entering a new data record to transfer field contents of several fields together into a separate field?
This should be done before saving the data record, possibly with the function "after_insert ($ data)" in order to avoid identical duplicate data records.
Do you have any tips on how I can do this?
Kind regards
Stefan
is there a possibility when entering a new data record to transfer field contents of several fields together into a separate field?
This should be done before saving the data record, possibly with the function "after_insert ($ data)" in order to avoid identical duplicate data records.
Do you have any tips on how I can do this?
Kind regards
Stefan
Re: Copie Data with after_insert_funktion
Hi,
can you be a bit more specific about what you try.
In the after_insert after_update hooks you can access all the data entered through your form. So you can use that data and populate another field (via sql) with that value.
can you be a bit more specific about what you try.
In the after_insert after_update hooks you can access all the data entered through your form. So you can use that data and populate another field (via sql) with that value.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Copie Data with after_insert_funktion
Can you explain what you try to reach?
Note that there is
If, however, you want to change data after insert, you cannot use
If this shall be done before saving a new record, you should do so inThis should be done before saving the data record
TABLENAME_before_insert
(not in TABLENAME_after_insert).Before you can modifyis there a possibility when entering a new data record to transfer field contents of several fields together into a separate field
$data
, for example join values of multiple fields onto one field. You can even check and validate data and conditionally deny insert by returning FALSE.Note that there is
&$data
-variable in before_insert
hook (variable passed by reference), which is different from _after_insert
hook. If, however, you want to change data after insert, you cannot use
$data
variable but you can write SQL commands and change data on database level directly. AppGini provides functions sqlValue()
and sql()
for manipulating data on db level. Note that $data
variable is passed by value in after_insert hook. Changes of $data array values will not effect data already stored at that moment.Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
Hello Jan,
thank you for the fast feedback.
I have a table with the fields Flst.Nr., Flst.UunterNr, Gemarkung, etc.
There is also a field with a combination of these three fields (= combination field), as this may only appear once in this table (the UNIQUE option is set). This field is currently filled in manually when creating a new record.
It is my goal that when entering the Flst.Nr, Flst.UnterNr. and Gemarkung a new data record, the combination field is automatically filled with the data.
Example:
Flst.Nr .: 1000
Flst.UnterNr .: 1
Gemarkung: Gemeinde
Combined field: 1000/1 Gemeinde
I hope I could explain it to some extent.
Stefan
thank you for the fast feedback.
I have a table with the fields Flst.Nr., Flst.UunterNr, Gemarkung, etc.
There is also a field with a combination of these three fields (= combination field), as this may only appear once in this table (the UNIQUE option is set). This field is currently filled in manually when creating a new record.
It is my goal that when entering the Flst.Nr, Flst.UnterNr. and Gemarkung a new data record, the combination field is automatically filled with the data.
Example:
Flst.Nr .: 1000
Flst.UnterNr .: 1
Gemarkung: Gemeinde
Combined field: 1000/1 Gemeinde
I hope I could explain it to some extent.
Stefan
Re: Copie Data with after_insert_funktion
as a starting point for your own development:
In before hook
In before hook
Code: Select all
$data['field3'] = $data['field1'] . ' / ' . $data['field2'];
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
... thanks for the code. I have adjusted the field names accordingly, see below:
Unfortunately, no data is transferred to the combi field when filling out the fields - have I overlooked something?
Code: Select all
function tb_flst_before_insert(&$data, $memberInfo, &$args) {
$data['flstugem'] = $data['flst_nr'] . '/' . $data['flst_u_nr'];
return TRUE;
}
Re: Copie Data with after_insert_funktion
Are you sure you have them in detail view UI? If you check [x] Hide field in detail view it will not be in UI and also no be in $data array.
You can try...
...in _before and/or _after hook to see available fields
You can try...
Code: Select all
var_dump($data);
exit;
...in _before and/or _after hook to see available fields
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
Good morning jan,
the field "flstugem" is not deactivated in the detail view. Unfortunately, I also don't get any feedback about the var_damp ($ data) command - nothing is displayed in the browser!
Kind regards
Stefan
the field "flstugem" is not deactivated in the detail view. Unfortunately, I also don't get any feedback about the var_damp ($ data) command - nothing is displayed in the browser!
Kind regards
Stefan
Re: Copie Data with after_insert_funktion
(1) It is
(2) And don't forget
var_dump
, not var_damp
(2) And don't forget
exit;
Last edited by jsetzer on 2021-12-18 10:53, edited 1 time in total.
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
..sorry, there was a typo, had
[code)var_dump ($ data);
exit;[/code]
entered, but without any output!
Kinde regards,
Stefan
[code)var_dump ($ data);
exit;[/code]
entered, but without any output!
Kinde regards,
Stefan
Re: Copie Data with after_insert_funktion
...no blank between $ and data!
Re: Copie Data with after_insert_funktion
Hello Jan,
have now created a new table with the fields 1, 2 and 3 and a try with the code
started. I activated the "UNIQUE" option for the field.
Seriously, when the button "Save Changes" is clicked, the data from fields 1 and 2 are transferred to field 3. I was of the opinion that when the field contents are entered, this is transferred.
When testing with a second data set with the same content, this message is consequently displayed:
Kind regards,
Stefan
have now created a new table with the fields 1, 2 and 3 and a try with the code
Code: Select all
$data['field3'] = $data['field1'] . ' / ' . $data['field2'];
Seriously, when the button "Save Changes" is clicked, the data from fields 1 and 2 are transferred to field 3. I was of the opinion that when the field contents are entered, this is transferred.
When testing with a second data set with the same content, this message is consequently displayed:
How can I better display this message so that a normal user can see it!Duplicate entry 'feld1 / feld2' for key 'testtable.field3_unique'
Query:
INSERT INTO `testtable` SET `field1`='feld1', `field2`='feld2', `field3`='feld1 / feld2'
The above info is displayed because you are currently signed in as the super admin. Other users won't see this.
You could try fixing this issue from the Admin Area : Utilities menu : View/Rebuild Fields.
Kind regards,
Stefan
Re: Copie Data with after_insert_funktion
By default, data of HTML forms (in the browser/client side) will be posted to the webserver on submit, then processed and perhaps stored in database on serverside. This is the way AppGini generated apps work with Detail View data.I was of the opinion that when the field contents are entered, this is transferred.
If, different from that above, you want serverside (PHP for example) processing, validation and/or storage (MySQL for example) on the fly (nearly real-time) when any user is changing data on client side, next to learning basic PHP and SQL, you will have to learn some javascript and AJAX and code this on your own. AppGini, by default, does not do this for us, but we can use hook functions for extending AppGini's standard functionality.
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
Hello Jan,
That's right ... what is important to me at the moment is that this solution effectively prevents a second record with the same combination of field contents from being stored in the database and an error being displayed to the user.
Even if the message should still be adjusted, it is still there - maybe I will find a nicer solution here!
I had an attempt with an AJAX and a javascript file in the hooks folder before, but sqlValue only works when the content is stored in the database, and that is too late.
Kind regards,
Stefan
That's right ... what is important to me at the moment is that this solution effectively prevents a second record with the same combination of field contents from being stored in the database and an error being displayed to the user.
Even if the message should still be adjusted, it is still there - maybe I will find a nicer solution here!
I had an attempt with an AJAX and a javascript file in the hooks folder before, but sqlValue only works when the content is stored in the database, and that is too late.
Kind regards,
Stefan
Re: Copie Data with after_insert_funktion
So, this means you want the check uniqueness of field3 value against database (serverside) on any change of any user at client side (browsers) before data got posted to server.
If the third field has been configured as unique (and is visible and editable in DV), there should be an automatic check by AppGini. This feature is quite new and should be available since 5.9x, if I remember right.
You should already see this feature when manually putting values into field3, which already exist in a different record of the same table and database. After input of duplicate value, there should be a hint next to the field which can be customized in language.php, I guess.
The idea is:
Using Javascript you can react to input changes of both input fields, then build the combined string and put it into the third field*. This should invoke an AJAX call with serverside check for uniqueness and give visual feedback in UI, if not unique. This should also deny inserting/updating the record.
* I'm not 100% sure if you have to trigger change event of third field by code or if val("..."); automatically triggers change event.
Known limits: in multiuser environment there is the risk that more than one user is inserting/updating records at nearly the same time. Check for uniqueness and display in UI can only return valid results at exactly that moment. It may be different a millisecond later. Unique constraint on DB table will deny duplicates in DB. But there may be an ugly error message, which should not be a problem to users in these rare cases.
If the third field has been configured as unique (and is visible and editable in DV), there should be an automatic check by AppGini. This feature is quite new and should be available since 5.9x, if I remember right.
You should already see this feature when manually putting values into field3, which already exist in a different record of the same table and database. After input of duplicate value, there should be a hint next to the field which can be customized in language.php, I guess.
The idea is:
Using Javascript you can react to input changes of both input fields, then build the combined string and put it into the third field*. This should invoke an AJAX call with serverside check for uniqueness and give visual feedback in UI, if not unique. This should also deny inserting/updating the record.
* I'm not 100% sure if you have to trigger change event of third field by code or if val("..."); automatically triggers change event.
Known limits: in multiuser environment there is the risk that more than one user is inserting/updating records at nearly the same time. Check for uniqueness and display in UI can only return valid results at exactly that moment. It may be different a millisecond later. Unique constraint on DB table will deny duplicates in DB. But there may be an ugly error message, which should not be a problem to users in these rare cases.
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Copie Data with after_insert_funktion
That's right. In field 3, unique is activated and, if entered manually, it is checked immediately with a corresponding response, see the following screenshot:
For the implementation with javascript and AXAL I would have to look for a corresponding example in the documentation - maybe you have an example code for me?
Kind Regards
Stefan
Of course, it would be great if a check was made for duplicates as soon as they were entered, before clicking save.For the implementation with javascript and AXAL I would have to look for a corresponding example in the documentation - maybe you have an example code for me?
Kind Regards
Stefan