Skip to content

Commit

Permalink
Evarisk#3982 [RiskAssessment] fix: make sure each risk has distinct a…
Browse files Browse the repository at this point in the history
…ppliedOn

In PHP, when you add an object to an array, the array holds a reference to that object, not a copy of it. So if you modify the object afterward, all references to that object reflect those changes.
To fix this, you need to clone the object before modifying it and adding it to the $risks array. This ensures that each entry in $risks is a separate object, and modifying one does not affect the others.
  • Loading branch information
evarisk-micka committed Jul 11, 2024
1 parent 84beddc commit f0f02d8
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions class/riskanalysis/risk.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,10 @@ public function fetchRisksOrderedByCotation($parent_id, $get_children_data = fal
foreach ($digiriskElementOfEntity->linkedObjectsIds['digiriskdolibarr_risk'] as $sharedRiskId) {
$sharedRisk = $riskList[$sharedRiskId];
if (is_object($sharedRisk)) {
$sharedRisk->appliedOn = $digiriskElementOfEntity->id;
$risks[] = $sharedRisk;
}
$clonedRisk = clone $sharedRisk;
$clonedRisk->appliedOn = $digiriskElementOfEntity->id;
$risks[] = $clonedRisk;
}
}
}
}
Expand Down

0 comments on commit f0f02d8

Please sign in to comment.