On Add - CurrentDateTime() and CurrentUserID()

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
alex
User
Posts: 267

On Add - CurrentDateTime() and CurrentUserID()

Post 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)


mobhar
User
Posts: 11727

Post by mobhar »

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


alex
User
Posts: 267

Post 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.


mobhar
User
Posts: 11727

Post by mobhar »

I thought you wrote the code in Row_Inserting server event.


alex
User
Posts: 267

Post 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!


mobhar
User
Posts: 11727

Post 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.


alex
User
Posts: 267

Post by alex »

Thank you!


Post Reply