f3-factory / fatfree-core

Fat-Free Framework core library
GNU General Public License v3.0
206 stars 89 forks source link

PHP 8.1 / Ad-Hoc field with NULL value #339

Closed wildtierschweiz closed 2 years ago

wildtierschweiz commented 2 years ago

https://github.com/bcosca/fatfree-core/blob/ee8ad6460ff33514a6b705cc8bff57e2b5048e3b/db/sql.php#L128

fatfree-core 3.8.0 / PHP 8.1 / MySQL-MariaDB

This code uses an Ad-Hoc field "max_sortorder"

<?php
// ad-hoc
$this->max_sortorder = 'MAX(sortorder)';
$this->load(['id_bill = ? AND id_product >= 0 AND is_deleted = 0', $id_bill_]);
$_result = (int)$this->max_sortorder + 1;
return $_result;

The Problem is, when the Ad-Hoc field results in NULL, when no record hydrades the mapper. The code fails in PHP 8.1, because the str_replace() call somehow gets passed NULL as the third parameter, which is depricated.

str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

I tried this to work around, but did not succeed:

<?php
// ad-hoc
$this->max_sortorder = 'COALESCE(MAX(sortorder), 0)';

My current work around that works is this:

<?php
if (!$this->count(['id_bill = ? AND id_product >= 0 AND is_deleted = 0', $id_bill_]))
    return 0;
// ad-hoc
$this->max_sortorder = 'MAX(sortorder)';
$this->load(['id_bill = ? AND id_product >= 0 AND is_deleted = 0', $id_bill_]);
$_result = (int)$this->max_sortorder + 1;
return $_result;
ikkez commented 2 years ago

Hi. I wasn't able to reproduce this. Perhaps a minor version issue or something. However, since https://github.com/bcosca/fatfree/issues/1252 also reported this same behaviour, I tried to patch this part slightly. Can you have a look at the latest commit and see if this solves the issue? thank you.