cakephp/cakephp · error · InvalidArgumentException
Invalid data array to nest.
Error message
Invalid data array to nest.
What it means
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.
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()
Example fix
// before $tree = Hash::nest($children); // root rows missing // after $all = array_merge($roots, $children); // $roots includes parent_id = null rows $tree = Hash::nest($all);
Defensive patterns
Strategy: validation
Validate before calling
if ($rows === []) {
return [];
}
$ids = array_column($rows, 'id');
$rootless = array_filter($rows, fn($r) => $r['parent_id'] !== null && !in_array($r['parent_id'], $ids, true));
if ($rootless) { /* fetch missing parents or fix data */ } Try / catch
try {
$tree = Hash::nest($rows);
} catch (\InvalidArgumentException $e) {
$tree = []; // or refetch full ancestor set
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot use path tokens of type
- `Hash::combine()` needs an equal number of keys + values.
- The hash type ` ` was not found. Available algorithms are…
- Invalid direction ` ` provided. Must be one of: 'desc'…
- Cannot find the cartesian product of a multidimensional…
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/14d0f87e7559f23c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Utility/Hash.php:1276
$result[$options['children']] = [];
$id = static::get($result, $idKeys);
$parentId = static::get($result, $parentKeys);
if (isset($idMap[$id][$options['children']])) {
$idMap[$id] = array_merge($result, $idMap[$id]);
} else {
$idMap[$id] = array_merge($result, [$options['children'] => []]);
}
if (!$parentId || !in_array($parentId, $ids)) {
$return[] = &$idMap[$id];
} else {
$idMap[$parentId][$options['children']][] = &$idMap[$id];
}
}
if (!$return) {
throw new InvalidArgumentException('Invalid data array to nest.');
}
if ($options['root']) {
$root = $options['root'];
} else {
$root = static::get($return[0], $parentKeys);
}
foreach ($return as $i => $result) {
$id = static::get($result, $idKeys);
$parentId = static::get($result, $parentKeys);
if ($id !== $root && $parentId !== $root) {
unset($return[$i]);
}
}
/** @var array<array> */
return array_values($return);View on GitHub (pinned to 1128eba9b0)