mvc: BaseModel - improve legacy mapper support when parent item doesn't exist.

Fixes:

ErrorException: Undefined array key 0 in /usr/local/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php:755
Stack trace:

If we can't find the specified root node, we should create one, which is similar to non legacy mapper nodes.
This commit is contained in:
Ad Schellevis 2026-02-03 16:28:06 +01:00
parent 5276f51dc0
commit 40cb82128d

View file

@ -752,7 +752,15 @@ abstract class BaseModel
* e.g. /system/user should place new entries at /system
**/
$pxpath = implode("/", array_slice(explode("/", $xpath), 0, -1));
$toDom = dom_import_simplexml($target_node->xpath($pxpath)[0]);
$target = $target_node->xpath($pxpath);
if (empty($target)) {
/* target doesn't exist yet, create the root node by traversing the tree (starting inside /opnsense) */
$target = $target_node;
foreach (array_slice(explode("/", trim($xpath, "/")), 1, -1) as $p) {
$target = count($target_node->xpath($p)) == 0 ? $target->addChild($p) : $target->xpath($p)[0];
}
}
$toDom = dom_import_simplexml($target[0]);
foreach ($newNodes as $node) {
if ($node !== null) {
$toDom->appendChild($toDom->ownerDocument->importNode($node, true));