{"record":{"id":"78c428eecb27cfbe","repo":"cakephp/cakephp","slug":"cannot-set-a-node-s-parent-as-itself","errorCode":null,"errorMessage":"Cannot set a node's parent as itself.","messagePattern":"Cannot set a node's parent as itself\\.","errorType":"exception","errorClass":"DatabaseException","httpStatus":null,"severity":"error","filePath":"src/ORM/Behavior/TreeBehavior.php","lineNumber":114,"sourceCode":"     * Transparently manages setting the lft and rght fields if the parent field is\n     * included in the parameters to be saved.\n     *\n     * @param \\Cake\\Event\\EventInterface<\\Cake\\ORM\\Table> $event The beforeSave event that was fired\n     * @param \\Cake\\Datasource\\EntityInterface $entity the entity that is going to be saved\n     * @return void\n     * @throws \\Cake\\Database\\Exception\\DatabaseException if the parent to set for the node is invalid\n     */\n    public function beforeSave(EventInterface $event, EntityInterface $entity): void\n    {\n        $isNew = $entity->isNew();\n        $config = $this->getConfig();\n        $parent = $entity->get($config['parent']);\n        $primaryKey = $this->_getPrimaryKey();\n        $dirty = $entity->isDirty($config['parent']);\n        $level = $config['level'];\n\n        if ($parent && $entity->get($primaryKey) === $parent) {\n            throw new DatabaseException(\"Cannot set a node's parent as itself.\");\n        }\n\n        if ($isNew) {\n            if ($parent) {\n                $parentNode = $this->_getNode($parent);\n                $edge = $parentNode->get($config['right']);\n                $entity->set($config['left'], $edge);\n                $entity->set($config['right'], $edge + 1);\n                $this->_sync(2, '+', \">= {$edge}\");\n\n                if ($level) {\n                    $entity->set($level, $parentNode[$level] + 1);\n                }\n\n                return;\n            }\n\n            $edge = $this->_getMax();","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/ORM/Behavior/TreeBehavior.php#L96-L132","documentation":"TreeBehavior::beforeSave() rejects a save where the entity's parent_id points at the entity itself, which would create a self-referencing cycle and corrupt the MPTT tree (left/right bounds). The save is aborted with a DatabaseException instead of persisting the invalid hierarchy.","triggerScenarios":"Thrown at src/ORM/Behavior/TreeBehavior.php:114 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not set the entity's parent field to its own primary key; omit the parent field or set it to a different node's id.","Validate the parent id against the entity's primary key before assigning it and before saving."],"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"}