After_Insert Not Working

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

After_Insert Not Working

Post by jimstoffel » 2023-10-18 14:30

Hello,
Been going through this forum and I thought I understood how to use the after_insert hook, but maybe not.
I have the following table called READINGS which have fields: readings_current, readings_previous, and readings_difference.
Am looking to have the field Readings_DIfference be calculated via the hook.
My code is:

sql("UPDATE `readings` set `readings_difference` = `readings_previous` - 'readings_current' where `id` = '{$data['readings_id']}'",$eo);

However after entering new data, the field readings_difference is blank.

What am I missing?

Thank you to any that can direct me in the right direction.

J.

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

Re: After_Insert Not Working

Post by jsetzer » 2023-10-18 15:12

Any errors in query log in Admin Area?
'readings_current'
You have to wrap fieldnames in backticks, not single quotes, as you have already done correctly here:
`readings_previous`. Try with `readings_current` instead of 'readings_current'.

Additionally, calculations may fail if any of these values is null. You can use ifnull(`fieldname`, 0) instead of `fieldname`.

Just a few first ideas.
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

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: After_Insert Not Working

Post by jimstoffel » 2023-10-18 15:24

Hi JSetzer,
Made the code change, however no go. And in the admin area, there was an 'error' with respect to the new 'data' needed an owner, which once assigned, still the field is blank.

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: After_Insert Not Working

Post by jimstoffel » 2023-10-18 17:38

Am wondering if this code would work too?


function readings_after_insert($data, $memberInfo, &$args) {
$data['readings_difference'] = $data['readings_previous'] - $data['readings_current'];
return TRUE;
}

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

Re: After_Insert Not Working

Post by jsetzer » 2023-10-18 17:54

You should do this in _before hook, not in _after hook, because changes on $data array will be stored after _before hook, but will not be stored again after _after hook.
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: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: After_Insert Not Working

Post by jsetzer » 2023-10-18 17:59

Tip: check the values!

Code: Select all


var_dump($data);

// do your calculations here

var_dump($data);
exit();
If those look correct, remove the var_dump- and exit-lines.
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

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: After_Insert Not Working

Post by jimstoffel » 2023-10-18 18:46

That worked - just have to figure out the correct calcs

(and better understand, the following:
changes on $data array will be stored after _before hook, but will not be stored again after _after hook.)

Thank you.
J.

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

Re: After_Insert Not Working

Post by jsetzer » 2023-10-19 12:46

and better understand, the following
Note the very little difference between _before- and _after-hook:

Code: Select all

function persons_before_insert(&$data, $memberInfo, &$args)
{
        // $data passed by reference
        // changes in $data[...] will be stored
	return TRUE;
}

function persons_after_insert($data, $memberInfo, &$args)
{ 
        // $data passed by value
	return TRUE;
}
Before

In _before-hook, $data-variable is passed by reference. Note the & character: &$data.

Here you can change the values of $data-array. AppGini will get back $data-variable and save the $data array's values to db afterwards, then call the _after-hook.

After

This time, AppGini fetches fresh data from database, then AppGini is passing $data-array by value, not by reference (without &).

You can do whatever you want with $data variable in _after-hook, for example do your own changes in database by using SQL commands, but after leaving your _after-hook, AppGini will not save changes you made to $data.

Actually, AppGini does not even know anything about your $data-changes outside _after-hook, because $data is passed by value, not passed by reference, which means $data is a (local) copy which can be seen inside your function, only.

On Insert

DV-Form ―POST➞ _before_insert(&$data) ➞ insert $data into DB by AppGini ➞ _after_insert($data) ➞ set record owner

On Update

DV-Form ―POST➞ _before_update(&$data) ➞ update $data in DB by AppGini ➞ _after_update($data)

So, if you want to make changes to $data and want AppGini to store them, always do this in before-hook. Use after-hook for additional tasks, for example for custom calculations based on previously stored values.

PS: Caution with before_delete and after_delete: after_delete the record will not be in database any more.
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

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: After_Insert Not Working

Post by jimstoffel » 2023-10-19 14:06

Thank you, jsetzer, for the explanation.
Am copying & pasting into my 'onenotes' for future reference!

Post Reply