Fix #36353 fresh object instance per iteration in getObjectsInCateg (#38748)

The fetch loop reused a single $tmpobj declared before the loop and
reset only $tmpobj->id before each fetch. Object classes whose fetch()
does not clear every property left stale data from previous rows, and
the shallow 'clone $tmpobj' kept nested object references (thirdparty,
extrafields, ...) pointing at the last fetched row.

Instantiate a fresh $tmpobj per row and append it directly so each
returned entry is fully independent.

Signed-off-by: Dolicraft <contact@dolicraft.com>
Co-authored-by: Dolicraft <contact@dolicraft.com>
This commit is contained in:
w3houde 2026-06-13 02:57:39 +02:00 committed by GitHub
parent 20943def41
commit 6df10d2b77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1050,11 +1050,11 @@ class Categorie extends CommonObject
if ($onlyids) {
$objs[] = $rec['fk_object'];
} else {
$tmpobj->id = 0;
$tmpobj->fetch($rec['fk_object']); // The fetch will erase $tmpobj->id only if it succeed.
$tmpobj = new $classnameforobj($this->db);
$tmpobj->fetch($rec['fk_object']); // The fetch will set $tmpobj->id only if it succeed.
// @phpstan-ignore-next-line
if ($tmpobj->id > 0) { // Failing fetch may happen for example when a category supplier was set and third party was moved as customer only. The object supplier can't be loaded.
$objs[] = clone $tmpobj;
$objs[] = $tmpobj;
}
}
}