doctrine/orm · error · Doctrine\ORM\Internal\TopologicalSort\CycleDetectedException

A cycle has been detected, so a topological sort is not poss

Error message

A cycle has been detected, so a topological sort is not possible. The getCycle() method provides the list of nodes that form the cycle.

What it means

Error "A cycle has been detected, so a topological sort is not possible. The getCycle() method provides the list of nodes that form the cycle." thrown in doctrine/orm.

Source

Thrown at src/Internal/TopologicalSort.php:110

     *
     * @return list<object>
     */
    public function sort(): array
    {
        foreach (array_keys($this->nodes) as $oid) {
            if ($this->states[$oid] === self::NOT_VISITED) {
                $this->visit($oid);
            }
        }

        return $this->sortResult;
    }

    private function visit(int $oid): void
    {
        if ($this->states[$oid] === self::IN_PROGRESS) {
            // This node is already on the current DFS stack. We've found a cycle!
            throw new CycleDetectedException($this->nodes[$oid]);
        }

        if ($this->states[$oid] === self::VISITED) {
            // We've reached a node that we've already seen, including all
            // other nodes that are reachable from here. We're done here, return.
            return;
        }

        $this->states[$oid] = self::IN_PROGRESS;

        // Continue the DFS downwards the edge list
        foreach ($this->edges[$oid] as $adjacentId => $optional) {
            try {
                $this->visit($adjacentId);
            } catch (CycleDetectedException $exception) {
                if ($exception->isCycleCollected()) {
                    // There is a complete cycle downstream of the current node. We cannot
                    // do anything about that anymore.

View on GitHub (pinned to d9b9ff7301)

When it happens

Trigger: Thrown at src/Internal/TopologicalSort.php:110 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of doctrine/orm@d9b9ff7301 (2026-08-21). Data as JSON: /api/errors/6b4b25261e47275b. Report an issue: GitHub.