Row_updating with database view

Hi,
I have a table ‘cmd’ containing 3 fields ‘validation’ (ENUM ‘Y’, ‘N’), ‘user_validation’ (INT) and ‘validation_date’ (DATETIME);
I’ve created a view ‘vv_cmd’ which is a “SELECT * FROM cmd”;
In PHPMaker 2022 I use this code in Row_Updating Server Event of ‘vv_cmd’ view :

// Row Updating event
function Row_Updating($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false
    if($rsold['validation'] != $rsnew['validation'] && $rsnew['validation'] == 'Y' ){
        $rsnew['user_validation'] = CurrentUserID();
        $rsnew['validation_date'] = CurrentDateTime();
    } else {
        $rsnew['user_validation'] = NULL;
        $rsnew['validation_date'] = NULL;
    }
    return true;
}

I want to update ‘user_validation’ and ‘validation_date’ when ‘validation’ changes to ‘Y’ but it don’t works with this code.
Can someone help me please to check what’s wrong ?

You better check if your condition is correct, you may debug by, e.g.

var_dump($rsold, $rsnew, $rsold['validation'] != $rsnew['validation'] && $rsnew['validation'] == 'Y');
die();

Hi,var_dump($rsold[‘validation’] != $rsnew[‘validation’] && $rsnew[‘validation’] == ‘Y’) returns “True” but

$rsnew[‘user_validation’] = CurrentUserID();
$rsnew[‘validation_date’] = CurrentDateTime();dont’t works !

You should enable Debug and check if there are SQL errors during update, enable:

  • Log SQL to file
  • Log error to file

Then both will be logged in the log file, check the log file for debug info, post the content for discussion.In your debug code (in the server event), your should check the data in $rsnew after your code also, e.g.

var_dump($rsold, $rsnew, $rsold['validation'] != $rsnew['validation'] && $rsnew['validation'] == 'Y'); // Before your code
// your code
var_dump($rsnew); // Check if your code did update $rsnew as you expected
die();

I notice that CurrentUserID() doesn’t work in my Project. I don’t no why ???Using this code works fine :

In User_Validated event :

Profile()->set("id", $rs["id"])
Profile()->save()

And in my Row_Updating event i change my code to :

// Row Updating event
function Row_Updating($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false
    if($rsold['validation'] != $rsnew['validation'] && $rsnew['validation'] == 'Y' ){
        $rsnew['user_validation'] = Profile()->get("id");
        $rsnew['validation_date'] = CurrentDateTime();
    } else {
        $rsnew['user_validation'] = NULL;
        $rsnew['validation_date'] = NULL;
    }
    return true;
}

I still wonder why CurrentUserID() doesn’t work in my Project ???

Make sure you have enabled User ID Security or there is no current User ID.