save the owner's username (not the logged user's username!) in a field

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

save the owner's username (not the logged user's username!) in a field

Post by fgazza » 2020-06-23 13:22

I would need a code that allows me to save the owner's username in the "user_name" field of my "privacy_consent" table and the "user" field the contents of the custom field ['username'] [0] always relative to the owner.
The problem is that the table already contains records and therefore the code must also be applied to records already created as the records are updated.
I thought that the problem could be solved by inserting this code in the hook file, in the "after update" function but this is not good because if the administrator changes the contents of the record the fields are updated with the administrator data while I need the "uer_name" and "user" fields kept the owner's data.

.. so this code doesn't work for my purpose:

Code: Select all

function my_table_after_insert($data, $memberInfo, &$args) {
		
		$mi = getMemberInfo();
		$data['user'] = $mi['custom'][0];
		
		$mi = getMemberInfo();
		$data['user_name'] = $mi['username'][0];

		return TRUE;
Thanks for your help.
Fabiano

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

Re: save the owner's username (not the logged user's username!) in a field

Post by jsetzer » 2020-06-23 14:15

Changing $data AFTER update will not effect the record in the database. Note, that $data is passed by value in after-hook and not by reference as in before-hook.

You can change $data in before hooks or you will have to use sql for changing the record after update.
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

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: save the owner's username (not the logged user's username!) in a field

Post by fgazza » 2020-06-23 15:45

Good afternoon Jan. I saw your kind reply in the forum.
I'm too inexperienced with the code to understand where to start.
Maybe I have to move into my code in the "before_insert" function?
In this case, since the insertion of the record takes place through an "automatic" code in an external (privacy consent) file, would the insertion of the following code work?

function my_table_before_insert ($ data, $ memberInfo, & $ args) {

$ mi = getMemberInfo ();
$ data ['user'] = $ mi ['custom'] [0];

$ mi = getMemberInfo ();
$ data ['user_name'] = $ mi ['username'] [0];

return TRUE;

THANKS AGAIN!
Fabiano

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

Re: save the owner's username (not the logged user's username!) in a field

Post by jsetzer » 2020-06-23 16:18

There are so many blanks in your code. Can you please always post code fragments wrapped in code tags.
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: 1814
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: save the owner's username (not the logged user's username!) in a field

Post by jsetzer » 2020-06-23 16:20

If you need the owner's memberID instead of the currently logged in memberID you must not use getMemberInfo().

The only way of getting a record's owner, as far as I know, is reading the data from membership_userrecords table.
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

Post Reply