Copie Data with after_insert_funktion

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Copie Data with after_insert_funktion

Post by skoch » 2021-12-17 08:55

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Copie Data with after_insert_funktion

Post by pbottcher » 2021-12-17 10:23

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.
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.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-17 10:27

Can you explain what you try to reach?
This should be done before saving the data record
If this shall be done before saving a new record, you should do so in TABLENAME_before_insert (not in TABLENAME_after_insert).
is there a possibility when entering a new data record to transfer field contents of several fields together into a separate field
Before you can modify $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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-17 10:59

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-17 13:38

as a starting point for your own development:

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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-17 16:12

... thanks for the code. I have adjusted the field names accordingly, see below:

Code: Select all

function tb_flst_before_insert(&$data, $memberInfo, &$args) {
		
		$data['flstugem'] = $data['flst_nr'] . '/' . $data['flst_u_nr'];

		return TRUE;
	}
Unfortunately, no data is transferred to the combi field when filling out the fields - have I overlooked something?

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-17 21:53

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...

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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-18 08:32

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-18 10:50

(1) It is 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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-18 10:52

skoch wrote:
2021-12-18 08:32
var_damp ($ data)
(3) Also, there must not be a blank after $

Code: Select all

var_dump($data);
exit;
Kind regards,
<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 readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-18 12:45

..sorry, there was a typo, had
[code)var_dump ($ data);
exit;[/code]
entered, but without any output!

Kinde regards,
Stefan

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-18 13:18

...no blank between $ and data!

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-18 15:37

Hello Jan,
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'];
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:
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.
How can I better display this message so that a normal user can see it!

Kind regards,
Stefan

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-19 09:11

I was of the opinion that when the field contents are entered, this is transferred.
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.

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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-19 14:02

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Copie Data with after_insert_funktion

Post by jsetzer » 2021-12-19 14:35

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.
Kind regards,
<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 readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

skoch
Veteran Member
Posts: 49
Joined: 2020-01-27 14:20

Re: Copie Data with after_insert_funktion

Post by skoch » 2021-12-20 08:19

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:
field3_unique.JPG
field3_unique.JPG (15.1 KiB) Viewed 3196 times
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

Post Reply