doctrine/orm tries to log NULL value via the DBAL logger #7252

Open
opened 2026-01-22 15:48:16 +01:00 by admin · 1 comment
Owner

Originally created by @rogervila on GitHub (Nov 20, 2023).

Bug Report

Q A
BC Break yes
Version 2.17.1

Summary

doctrine/orm tries to log a null value via the DBAL logger when using \Doctrine\ORM\Query@getSingleResult() after upgrading from 2.16.3 to 2.17.1

Current behavior

doctrine/orm tries to log a null value via the DBAL logger when using \Doctrine\ORM\Query@getSingleResult()

This is the error trace:

{
    "type": "https://tools.ietf.org/html/rfc2616#section-10",
    "title": "An error occurred",
    "status": 500,
    "detail": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger::log(): Argument #1 ($message) must be of type string, null given, called in /var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php on line 43",
    "class": "TypeError",
    "trace": [
        {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "/var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php",
            "line": 58,
            "args": []
        },
        {
            "namespace": "Symfony\\Bridge\\Doctrine\\Logger",
            "short_class": "DbalLogger",
            "class": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger",
            "type": "->",
            "function": "log",
            "file": "/var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php",
            "line": 43,
            "args": []
        },
        {
            "namespace": "Symfony\\Bridge\\Doctrine\\Logger",
            "short_class": "DbalLogger",
            "class": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger",
            "type": "->",
            "function": "startQuery",
            "file": "/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/LoggerChain.php",
            "line": 47,
            "args": []
        },
        {
            "namespace": "Doctrine\\DBAL\\Logging",
            "short_class": "LoggerChain",
            "class": "Doctrine\\DBAL\\Logging\\LoggerChain",
            "type": "->",
            "function": "startQuery",
            "file": "/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php",
            "line": 1294,
            "args": []
        },
        {
            "namespace": "Doctrine\\DBAL",
            "short_class": "Connection",
            "class": "Doctrine\\DBAL\\Connection",
            "type": "->",
            "function": "executeQuery",
            "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Exec/SingleSelectExecutor.php",
            "line": 33,
            "args": []
        },
        {
            "namespace": "Doctrine\\ORM\\Query\\Exec",
            "short_class": "SingleSelectExecutor",
            "class": "Doctrine\\ORM\\Query\\Exec\\SingleSelectExecutor",
            "type": "->",
            "function": "execute",
            "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php",
            "line": 327,
            "args": []
        },
        {
            "namespace": "Doctrine\\ORM",
            "short_class": "Query",
            "class": "Doctrine\\ORM\\Query",
            "type": "->",
            "function": "_doExecute",
            "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php",
            "line": 1212,
            "args": []
        },
        {
            "namespace": "Doctrine\\ORM",
            "short_class": "AbstractQuery",
            "class": "Doctrine\\ORM\\AbstractQuery",
            "type": "->",
            "function": "executeIgnoreQueryCache",
            "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php",
            "line": 1166,
            "args": []
        },
        {
            "namespace": "Doctrine\\ORM",
            "short_class": "AbstractQuery",
            "class": "Doctrine\\ORM\\AbstractQuery",
            "type": "->",
            "function": "execute",
            "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php",
            "line": 991,
            "args": []
        },
        {
            "namespace": "Doctrine\\ORM",
            "short_class": "AbstractQuery",
            "class": "Doctrine\\ORM\\AbstractQuery",
            "type": "->",
            "function": "getSingleResult",
            "file": "MY PROJECT FILE THAT USES \\Doctrine\\ORM\\Query@getSingleResult()",
            "line": 66,
            "args": []
        }
]

How to reproduce

This is the class that triggers the error (names have been modified)

<?php

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;

class FooRepository
{
    /**
     * @param EntityRepository<Foo> $doctrineFooRepository
     */
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly EntityRepository $doctrineFooRepository
    ) {
    }

    public function getOneByBar(Bar $bar): Foo
    {
        $qb = $this->doctrineFooRepository->createQueryBuilder('i');
        $qb->join(
            //
        )
            ->setParameter('bar', $bar)
        ;
        $query = $qb->getQuery();

        try {
            $result = $query->getSingleResult(); // ERROR HAPPENS HERE
        } catch (\Throwable $e) {
            throw new FooNotFound($e);
        }

        return $result;
    }

    public function find(string $id): ?Foo
    {
        $foo = $this->doctrineFooRepository->find($id);

        return $foo instanceof Foo ? $foo : null;
    }

    public function add(Foo $foo): void
    {
        $this->entityManager->persist($foo);
        $this->entityManager->flush();
    }
}

Expected behavior

doctrine/orm should not try to log a NULL value since the logger expects string values.

Originally created by @rogervila on GitHub (Nov 20, 2023). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes | Version | 2.17.1 #### Summary doctrine/orm tries to log a null value via the DBAL logger when using \\Doctrine\\ORM\\Query@getSingleResult() after upgrading from 2.16.3 to 2.17.1 #### Current behavior <!-- What is the current (buggy) behavior? --> doctrine/orm tries to log a null value via the DBAL logger when using \\Doctrine\\ORM\\Query@getSingleResult() This is the error trace: ```json { "type": "https://tools.ietf.org/html/rfc2616#section-10", "title": "An error occurred", "status": 500, "detail": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger::log(): Argument #1 ($message) must be of type string, null given, called in /var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php on line 43", "class": "TypeError", "trace": [ { "namespace": "", "short_class": "", "class": "", "type": "", "function": "", "file": "/var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php", "line": 58, "args": [] }, { "namespace": "Symfony\\Bridge\\Doctrine\\Logger", "short_class": "DbalLogger", "class": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger", "type": "->", "function": "log", "file": "/var/www/vendor/symfony/doctrine-bridge/Logger/DbalLogger.php", "line": 43, "args": [] }, { "namespace": "Symfony\\Bridge\\Doctrine\\Logger", "short_class": "DbalLogger", "class": "Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger", "type": "->", "function": "startQuery", "file": "/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/LoggerChain.php", "line": 47, "args": [] }, { "namespace": "Doctrine\\DBAL\\Logging", "short_class": "LoggerChain", "class": "Doctrine\\DBAL\\Logging\\LoggerChain", "type": "->", "function": "startQuery", "file": "/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php", "line": 1294, "args": [] }, { "namespace": "Doctrine\\DBAL", "short_class": "Connection", "class": "Doctrine\\DBAL\\Connection", "type": "->", "function": "executeQuery", "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Exec/SingleSelectExecutor.php", "line": 33, "args": [] }, { "namespace": "Doctrine\\ORM\\Query\\Exec", "short_class": "SingleSelectExecutor", "class": "Doctrine\\ORM\\Query\\Exec\\SingleSelectExecutor", "type": "->", "function": "execute", "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php", "line": 327, "args": [] }, { "namespace": "Doctrine\\ORM", "short_class": "Query", "class": "Doctrine\\ORM\\Query", "type": "->", "function": "_doExecute", "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php", "line": 1212, "args": [] }, { "namespace": "Doctrine\\ORM", "short_class": "AbstractQuery", "class": "Doctrine\\ORM\\AbstractQuery", "type": "->", "function": "executeIgnoreQueryCache", "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php", "line": 1166, "args": [] }, { "namespace": "Doctrine\\ORM", "short_class": "AbstractQuery", "class": "Doctrine\\ORM\\AbstractQuery", "type": "->", "function": "execute", "file": "/var/www/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php", "line": 991, "args": [] }, { "namespace": "Doctrine\\ORM", "short_class": "AbstractQuery", "class": "Doctrine\\ORM\\AbstractQuery", "type": "->", "function": "getSingleResult", "file": "MY PROJECT FILE THAT USES \\Doctrine\\ORM\\Query@getSingleResult()", "line": 66, "args": [] } ] ``` #### How to reproduce This is the class that triggers the error (names have been modified) ```php <?php use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; class FooRepository { /** * @param EntityRepository<Foo> $doctrineFooRepository */ public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntityRepository $doctrineFooRepository ) { } public function getOneByBar(Bar $bar): Foo { $qb = $this->doctrineFooRepository->createQueryBuilder('i'); $qb->join( // ) ->setParameter('bar', $bar) ; $query = $qb->getQuery(); try { $result = $query->getSingleResult(); // ERROR HAPPENS HERE } catch (\Throwable $e) { throw new FooNotFound($e); } return $result; } public function find(string $id): ?Foo { $foo = $this->doctrineFooRepository->find($id); return $foo instanceof Foo ? $foo : null; } public function add(Foo $foo): void { $this->entityManager->persist($foo); $this->entityManager->flush(); } } ``` #### Expected behavior <!-- What was the expected (correct) behavior? --> doctrine/orm should not try to log a NULL value since the logger expects string values.
Author
Owner

@greg0ire commented on GitHub (Nov 20, 2023):

This looks related to https://github.com/doctrine/orm/pull/11027… what query cache do you use? Do you reproduce this on your development environment?

@greg0ire commented on GitHub (Nov 20, 2023): This looks related to https://github.com/doctrine/orm/pull/11027… what query cache do you use? Do you reproduce this on your development environment?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7252