I want to rename a image during upload, so the user can select "any” file and in Row_Updating I change the filename to the name I need (based on database content).
In PHPMaker 2022 I was able to do this with the following code:
// Empty DbValue if the uploaded file has the same name as the target file, otherwise it will be deleted
if ($this->imagefield->Upload->DbValue == $new_filename) $this->imagefield->Upload->DbValue="";
// Set new filename
$rsnew['imagefield'] = $new_filename;
$this->imagefield->FileName = $new_filename;
However this code no longer works in PHPMaker 2026. Does anybody know how to do this in 2026?
If I am not wrong, versions prior to v2025 had
// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
// Enter your code here
// To cancel, set return value to false
return true;
}
which in v2025 has been changed to:
// Row Inserting event
function Row_Inserting(?array $oldRow, array &$newRow): ?bool
{
// Enter your code here
// To cancel, set return value to false
// To skip for grid insert/update, set return value to null
return true;
}
and in v2026 is:
// Row Inserting event
function Row_Inserting(?BaseEntity $oldRow, BaseEntity $newRow): ?bool
{
// Enter your code here
// To cancel, set return value to false
// To skip for grid insert/update, set return value to null
return true;
}
So, maybe the problem is probably coming from using $rsnew instead of $newRow
In my case, I solved it with:
$customImageName = "whatEverTheImageNameIs";
if ($this->imagefield->Upload->DbValue === $customImageName) {
$this->imagefield->Upload->DbValue=null;
}
if (!empty($newRow['imagefield'])) {
$newRow['imagefield'] = $customImageName;
}
The issue I observed is that, although the "Delete file on update/delete" option is checked, when deleting the file, the database value is set to null, but the file is not deleted…
Please do not clear the DbValue in your Row_Updating server event (which is needed for deleting the uploaded file).
The following seems to work:
if (isset($this->$imageField) && $this->$imageField->Upload->FileSize > 0) {
if($customImageName === $this->$imageField->Upload->DbValue) {
$this->$imageField->Upload->DbValue = null;
}
$newRow['imagefield'] = $customImageName;
}
You're right! I tested it in the test project and it worked for me too.
However, your solution seems to work in all cases! I'll try implementing it in the final project.
Thank you!
1 Like