cakephp/cakephp · error · DatabaseException

Cannot use node ` ` as parent for entity ` `.

Error message

Cannot use node `%s` as parent for entity `%s`.

What it means

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.

Solutions

  1. Choose a parent node that is not a descendant (or the node itself) of the entity being re-parented.
  2. Validate before moving: the target parent's lft/rght values must lie entirely outside the entity's own lft/rght range.
  3. If moving a subtree, first move the intended parent out of the subtree, or restructure the operation.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/ORM/Behavior/TreeBehavior.php:284 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/3a14e3e3a6a6b8db. Report an issue: GitHub.

Appendix: source

Thrown at src/ORM/Behavior/TreeBehavior.php:284

     * move can be done without corrupting the structure.
     *
     * @param \Cake\Datasource\EntityInterface $entity The entity to re-parent
     * @param mixed $parent the id of the parent to set
     * @return void
     * @throws \Cake\Database\Exception\DatabaseException if the parent to set to the entity is not valid
     */
    protected function _setParent(EntityInterface $entity, mixed $parent): void
    {
        $config = $this->getConfig();
        $parentNode = $this->_getNode($parent);
        $this->_ensureFields($entity);
        $parentLeft = $parentNode->get($config['left']);
        $parentRight = $parentNode->get($config['right']);
        $right = $entity->get($config['right']);
        $left = $entity->get($config['left']);

        if ($parentLeft > $left && $parentLeft < $right) {
            throw new DatabaseException(sprintf(
                'Cannot use node `%s` as parent for entity `%s`.',
                $parent,
                $entity->get($this->_getPrimaryKey()),
            ));
        }

        // Values for moving to the left
        $diff = $right - $left + 1;
        $targetLeft = $parentRight;
        $targetRight = $diff + $parentRight - 1;
        $min = $parentRight;
        $max = $left - 1;

        if ($left < $targetLeft) {
            // Moving to the right
            $targetLeft = $parentRight - $diff;
            $targetRight = $parentRight - 1;
            $min = $right + 1;

View on GitHub (pinned to 1128eba9b0)