{"record":{"id":"14d0f87e7559f23c","repo":"cakephp/cakephp","slug":"invalid-data-array-to-nest","errorCode":null,"errorMessage":"Invalid data array to nest.","messagePattern":"Invalid data array to nest\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Utility/Hash.php","lineNumber":1276,"sourceCode":"            $result[$options['children']] = [];\n\n            $id = static::get($result, $idKeys);\n            $parentId = static::get($result, $parentKeys);\n\n            if (isset($idMap[$id][$options['children']])) {\n                $idMap[$id] = array_merge($result, $idMap[$id]);\n            } else {\n                $idMap[$id] = array_merge($result, [$options['children'] => []]);\n            }\n            if (!$parentId || !in_array($parentId, $ids)) {\n                $return[] = &$idMap[$id];\n            } else {\n                $idMap[$parentId][$options['children']][] = &$idMap[$id];\n            }\n        }\n\n        if (!$return) {\n            throw new InvalidArgumentException('Invalid data array to nest.');\n        }\n\n        if ($options['root']) {\n            $root = $options['root'];\n        } else {\n            $root = static::get($return[0], $parentKeys);\n        }\n\n        foreach ($return as $i => $result) {\n            $id = static::get($result, $idKeys);\n            $parentId = static::get($result, $parentKeys);\n            if ($id !== $root && $parentId !== $root) {\n                unset($return[$i]);\n            }\n        }\n\n        /** @var array<array> */\n        return array_values($return);","sourceCodeStart":1258,"sourceCodeEnd":1294,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Utility/Hash.php#L1258-L1294","documentation":"Hash::nest() builds a tree from a flat list using $options['idPath'] and $options['parentPath']. It throws InvalidArgumentException when, after linking children to parents, no root node(s) were produced — i.e. $return is empty because every row references a parent that is not present in the data (and is not null/zero), or the input was empty/malformed.","triggerScenarios":"Passing an empty array; rows whose parent values never match any id in the set (e.g. missing root row); idPath/parentPath configured wrong so no row resolves to a null/missing parent; parent ids referencing deleted records.","commonSituations":"Flattened category/comment tables where the root record was excluded by a WHERE clause; soft-deleted parent rows filtered out; wrong field names in idPath/parentPath; data returned from a scoped query missing ancestors.","solutions":["Include the root row(s) (parent = null/0) in the dataset before nesting","Verify idPath and parentPath options point to real fields, e.g. 'id' and 'parent_id'","Check that all parent values exist as ids in the input","Handle empty input before calling Hash::nest()"],"exampleFix":"// before\n$tree = Hash::nest($children); // root rows missing\n// after\n$all = array_merge($roots, $children); // $roots includes parent_id = null rows\n$tree = Hash::nest($all);","handlingStrategy":"validation","validationCode":"if ($rows === []) {\n    return [];\n}\n$ids = array_column($rows, 'id');\n$rootless = array_filter($rows, fn($r) => $r['parent_id'] !== null && !in_array($r['parent_id'], $ids, true));\nif ($rootless) { /* fetch missing parents or fix data */ }","typeGuard":null,"tryCatchPattern":"try {\n    $tree = Hash::nest($rows);\n} catch (\\InvalidArgumentException $e) {\n    $tree = []; // or refetch full ancestor set\n}","preventionTips":["Always include root rows (parent null/0) in nested datasets","Verify idPath/parentPath field names match your data","Refetch ancestors when queries are scoped/filtered","Handle empty result sets before nesting"],"tags":["php","cakephp","hash","nest","tree"],"backgroundTag":"invalid-argument-value","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}