{"record":{"id":"3a14e3e3a6a6b8db","repo":"cakephp/cakephp","slug":"cannot-use-node-s-as-parent-for-entity-s","errorCode":null,"errorMessage":"Cannot use node `%s` as parent for entity `%s`.","messagePattern":"Cannot use node `(.+?)` as parent for entity `(.+?)`\\.","errorType":"exception","errorClass":"DatabaseException","httpStatus":null,"severity":"error","filePath":"src/ORM/Behavior/TreeBehavior.php","lineNumber":284,"sourceCode":"     * move can be done without corrupting the structure.\n     *\n     * @param \\Cake\\Datasource\\EntityInterface $entity The entity to re-parent\n     * @param mixed $parent the id of the parent to set\n     * @return void\n     * @throws \\Cake\\Database\\Exception\\DatabaseException if the parent to set to the entity is not valid\n     */\n    protected function _setParent(EntityInterface $entity, mixed $parent): void\n    {\n        $config = $this->getConfig();\n        $parentNode = $this->_getNode($parent);\n        $this->_ensureFields($entity);\n        $parentLeft = $parentNode->get($config['left']);\n        $parentRight = $parentNode->get($config['right']);\n        $right = $entity->get($config['right']);\n        $left = $entity->get($config['left']);\n\n        if ($parentLeft > $left && $parentLeft < $right) {\n            throw new DatabaseException(sprintf(\n                'Cannot use node `%s` as parent for entity `%s`.',\n                $parent,\n                $entity->get($this->_getPrimaryKey()),\n            ));\n        }\n\n        // Values for moving to the left\n        $diff = $right - $left + 1;\n        $targetLeft = $parentRight;\n        $targetRight = $diff + $parentRight - 1;\n        $min = $parentRight;\n        $max = $left - 1;\n\n        if ($left < $targetLeft) {\n            // Moving to the right\n            $targetLeft = $parentRight - $diff;\n            $targetRight = $parentRight - 1;\n            $min = $right + 1;","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/ORM/Behavior/TreeBehavior.php#L266-L302","documentation":"TreeBehavior::_setParent() fetched the candidate parent node and validated the move against the tree's left/right bounds; the requested parent is not a valid target (e.g. a descendant of the entity, which would create a cycle). The reparenting is aborted to keep the MPTT structure intact.","triggerScenarios":"Thrown at src/ORM/Behavior/TreeBehavior.php:284 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Choose a parent node that is not a descendant (or the node itself) of the entity being re-parented.","Validate before moving: the target parent's lft/rght values must lie entirely outside the entity's own lft/rght range.","If moving a subtree, first move the intended parent out of the subtree, or restructure the operation."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}