Assingning ownership to automatically created records

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
wilmira
Veteran Member
Posts: 75
Joined: 2013-07-11 18:00

Assingning ownership to automatically created records

Post by wilmira » 2024-07-12 00:37

Hello everybody,

I have succesfully solved this problem using the solution found here: https://forums.appgini.com/phpbb/viewtopic.php?t=2524

However the code does not work if I use the return FALSE. Here is my code:

Code: Select all

function espacios_after_insert($data, $memberInfo, &$args) {
	$memberid1 = ($memberInfo['username']);	
	$num_esp = $data['num_esp'];
	
	sqlvalue("INSERT INTO ocupacion (ing_ocupa, sal_ocupa, disp_ocupa, usid_ocupa) VALUES (0, 0, '$num_esp', '$memberid1')");
	/*Assigning the record to the user*/
		$id_ocupa=sqlValue("select `id_ocupa` from `ocupacion` where `usid_ocupa`='$memberid1'");
		set_record_owner("ocupacion", $id_ocupa, $memberid1);
		return TRUE;
	}
In order to avoid any issues it was recomended in the post https://forums.appgini.com/phpbb/viewtopic.php?t=2524 that I should return FALSE my code, however it won´t work if I do that. What happens when I use return FALSE is that I do assing the owner to the new record on the table "ocupacion", but the record in the table "espacios" is created without an owner.

Should I expect any issues when return FALSE is not used?

Thanks in advance for your help.

Wilfredo Mira

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

Re: Assingning ownership to automatically created records

Post by pbottcher » 2024-07-12 07:59

Hi,

maybe you can try:

Code: Select all

function espacios_after_insert($data, $memberInfo, &$args) {
	$memberid1 = ($memberInfo['username']);	
	$num_esp = $data['num_esp'];
	
	sqlvalue("INSERT INTO ocupacion (ing_ocupa, sal_ocupa, disp_ocupa, usid_ocupa) VALUES (0, 0, '$num_esp', '$memberid1')");
	/*Assigning the record to the user*/
		$id_ocupa = db_insert_id(db_link());
		set_record_owner("ocupacion", $id_ocupa, $memberid1);
		return TRUE;
	}
Not tested, but you will figure out quickly :-)
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.

wilmira
Veteran Member
Posts: 75
Joined: 2013-07-11 18:00

Re: Assingning ownership to automatically created records

Post by wilmira » 2024-07-12 17:02

Hi Pbottcher,

Thank you for your answer, that is the same code I am using and it is working, I am just concern about closing the code with return TRUE could cause some issues later on, because it was recommended to close the code with return FALSE.

Thanks again for your reply

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

Re: Assingning ownership to automatically created records

Post by pbottcher » 2024-07-13 06:24

Hi,

as I did not try this scenaria, I'm not sure, why you would return FALSE, as returning FALSE would indicate that the record was not saved in the database, whereas it was in reality. Would be missleading. So as long as it works, no need to change it.
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
a.gneady
Site Admin
Posts: 1320
Joined: 2012-09-27 14:46
Contact:

Re: Assingning ownership to automatically created records

Post by a.gneady » 2024-07-13 14:59

The after_insert hook should return false only if you don't want to automatically set the owner of the record that has been inserted into your current table, espacios in this example. But in your case, you are inserting another record into the ocupacion table and setting its owner. In that case, the right step is to return TRUE; to containue with setting the owner of the newly-inserted espacios record.

I hope that helps.
:idea: AppGini plugins to add more power to your apps:

wilmira
Veteran Member
Posts: 75
Joined: 2013-07-11 18:00

Re: Assingning ownership to automatically created records

Post by wilmira » 2024-07-15 22:32

Hello Ahmed,

Thank you very much, your answer is indeed helpful.

Post Reply