Row_Inserting $rsold not accessible

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

Row_Inserting $rsold not accessible

Post by dquinlan »

I am trying to override a status field when copying a record.

The following simple code in Row_Inserting causes an error when Copying that I can't trap

// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false
    if ($rsold["Status"]=="Issued") {
       	$rsnew["Status"]="Draft";
    }
	Log("Old Status " . $rsold["Status"]);
	Log("New Status " . $rsnew["Status"]);
    return true;
}

If I add a new record, it is saved and log is:

[2022-07-26T00:23:58.080070+00:00] log.DEBUG: Old Status [] []
[2022-07-26T00:23:58.080336+00:00] log.DEBUG: New Status Draft [] []

If I Copy and save a record I get

Error
An internal error has occurred while processing your request.

and no logs or console messages. Using V2022.12

Any thoughts?


arbei
User
Posts: 9381

Post by arbei »

The $rsold only exists when you copy a record, when you insert a new record, there is no old record, so in your code you should check if ($rsold) first.


dquinlan
User
Posts: 29

Post by dquinlan »

Sorry I didn't explain that well, I understand rsold doesn't exist on Add.
My issue is I can't access rsold on Copy - it crashes?


arbei
User
Posts: 9381

Post by arbei »

You may enable debug and check the actual PHP error message.


dquinlan
User
Posts: 29

Post by dquinlan »

Enable Debug did display error detail

/opt/bitnami/apache/htdocs/models/Invoices.php(2568): Cannot use object of type PHPMaker2022\stables\Recordset as array

I don't understand, because I can operate on $rsnew on Row_inserting e.g

public function rowInserting($rsold, &$rsnew)
{
   	$rsnew["Status"]="Draft";
	Log("New Status " . $rsnew["Status"]);
    return true;
}

I can also operate on both $rsold and $rsnew in Row_Updated function

I just can't operate on $rsold in Row_Inserting when copying a record?


arbei
User
Posts: 9381

Post by arbei »

As the error said, the $rsold is Recordset, not array, so you may check first, e.g.

if ($rsold && property_exists($rsold, "fields")) { // Recordset
	$rsold = $rsold->fields;
}
$myValue = $rsold["MyFieldName"];

dquinlan
User
Posts: 29

Post by dquinlan »

That is the answer, many thanks

On Row_Inserting $rsold is a recordset and $rsnew is an array.

	$oldstat = $rsold->fields["Status"];
	if ($oldstat=="Issued") {
             	$rsnew["Status"]="Draft";
	}

While on Row_Updated both are arrays.


Post Reply