Page 1 of 1

On Add - CurrentDateTime() and CurrentUserID()

Posted: Thu Feb 15, 2024 11:51 pm
by alex

Hello, lets say I have two tables: "orderDetails" and "Orders". Table "Orders" has fields "date_added" and "user_added" which are set as "CurrentDateTime()" and "CurrentUserID()" in "Auto-Update Value" of table settings. If I add record to table "Orders" then "date_added" and "user_added" are automatically inserted.

Table "orderDetail" has field "orderId" and "Allow add" option (all fields of table "orders" selected).

The problem is that "Allow add" option inserts information to table "orders" except Auto-Updatable fields "date_added" and "user_added" - leaves empty.

Please advise how to solve. (v2024.8)


Re: onAdd - CurrentDateTime() CurrentUserID() - v2024.8

Posted: Fri Feb 16, 2024 12:20 am
by mobhar

You may simply use Row_Inserting server event in order to insert each of those 2 fields respectively.


Re: onAdd - CurrentDateTime() CurrentUserID() - v2024.8

Posted: Fri Feb 16, 2024 1:27 am
by alex

Thank you! By the way insertion of "CurrentDateTime()" and "CurrentUserID()" works for admin only (id = "-1") but not for any other users even if they had Administrators permissions.

I select the field name that you would like the CurrentDateTime() and CurrentUserID() to be reflected from field panel, then on the add page panel, input CurrentDateTime() or CurrentUserID() as default value.


Re: On Add - CurrentDateTime() and CurrentUserID()

Posted: Fri Feb 16, 2024 10:01 am
by mobhar

I thought you wrote the code in Row_Inserting server event.


Re: onAdd - CurrentDateTime() CurrentUserID() - v2024.8

Posted: Sun Feb 18, 2024 1:09 am
by alex

alex wrote:

insertion of "CurrentDateTime()" and "CurrentUserID()" works for admin only (id = "-1") but not for any other users even if they had Administrators permissions.

I meant phpmakers built in option behavior ("Auto-Update Value" in table settings as "CurrentDateTime()" and "CurrentUserID()") with dynamic userlevels. I wrote this as a message for developers may be.

Here is my code. It works ok:

// Row Inserted event
public function rowInserted($rsold, $rsnew)
{
	 $check_user_added = ExecuteScalar("select user_added from `investors` WHERE id = ".$rsnew["id"] ); // from ".CurrentTableName()." instead of `investors` doesnt work here
    
	if (!$check_user_added) { // additopnal checking in case this bug will be fixed in newer versions of phpmaker
		$AddOptionPage = "update `investors` set user_added = '".CurrentUserID()."', date_added = '".CurrentDateTime()."' WHERE id = '".$rsnew["id"]."' and user_added is null"; // update ".CurrentTableName()." instead of `investors` doesnt work here
		ExecuteStatement($AddOptionPage);
	}
}

Thank you!


Re: On Add - CurrentDateTime() and CurrentUserID()

Posted: Sun Feb 18, 2024 11:53 pm
by mobhar

Or, alternatively, you may simply use Row_Inserting server event, as I mentioned before, just assign the fields with those global functions, for example:

$rsnew["date_added"] = CurrentDateTime();
$rsnew["user_added"] = CurrentUserID();

In other words, there is no need to check the record first in Row_Inserted server event and then update those fields. All you need to do is by assigning those fields with those global functions respectively.


Re: On Add - CurrentDateTime() and CurrentUserID()

Posted: Mon Feb 19, 2024 1:51 am
by alex

Thank you!