Page 1 of 1

After_Insert Not Working

Posted: 2023-10-18 14:30
by jimstoffel
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.

Re: After_Insert Not Working

Posted: 2023-10-18 15:12
by jsetzer
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.

Re: After_Insert Not Working

Posted: 2023-10-18 15:24
by jimstoffel
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.

Re: After_Insert Not Working

Posted: 2023-10-18 17:38
by jimstoffel
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;
}

Re: After_Insert Not Working

Posted: 2023-10-18 17:54
by jsetzer
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.

Re: After_Insert Not Working

Posted: 2023-10-18 17:59
by jsetzer
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.

Re: After_Insert Not Working

Posted: 2023-10-18 18:46
by jimstoffel
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.

Re: After_Insert Not Working

Posted: 2023-10-19 12:46
by jsetzer
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.

Re: After_Insert Not Working

Posted: 2023-10-19 14:06
by jimstoffel
Thank you, jsetzer, for the explanation.
Am copying & pasting into my 'onenotes' for future reference!