Page 1 of 1

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

Posted: 2020-06-23 13:22
by fgazza
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

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

Posted: 2020-06-23 14:15
by jsetzer
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.

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

Posted: 2020-06-23 15:45
by fgazza
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

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

Posted: 2020-06-23 16:18
by jsetzer
There are so many blanks in your code. Can you please always post code fragments wrapped in code tags.

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

Posted: 2020-06-23 16:20
by jsetzer
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.