Error handling when misuse of find() with array values #7312

Open
opened 2026-01-22 15:49:49 +01:00 by admin · 0 comments
Owner

Originally created by @symfonyaml on GitHub (Feb 7, 2024).

Bug Report

Q A
BC Break no
Version 2.18.x

Summary

Misuse of $em->find(...) with array as identifier.

Current behavior

$em->find(
    Test::class,
    ['id' => [1234]], // <----- Note the identifier is an array, which is wrong
);
// returns the following PHP warning :
// PHP Warning:  Array to string conversion in UnitOfWork.php on line 1560

How to reproduce

  • Create a new directory
    mkdir ./reproduce-doctrine-bug
    cd ./reproduce-doctrine-bug
    
  • Install the doctrine/orm and symfony/cache repos
    composer require doctrine/orm:^2.18 symfony/cache
    
  • Create a Test entity
    mkdir Entity
    echo "<?php
    namespace App\Entity;
    use Doctrine\ORM\Mapping as ORM;
    #[ORM\Entity]
    class Test
    {
      #[ORM\Column(name: 'id', type: 'integer')]
      #[ORM\Id]
      #[ORM\GeneratedValue(strategy: 'AUTO')]
      private \$id;
    }
    " > Entity/Test.php
    
  • Create the PHP script test.php to throw the error
    echo "<?php // reproduce.php
    require_once 'vendor/autoload.php';
    require_once 'Entity/Test.php';
    \$connection = \Doctrine\DBAL\DriverManager::getConnection(
      [
          'driver' => 'pdo_sqlite',
          'path' => tempnam(sys_get_temp_dir(), 'doctrine') . '.sqlite',
          'memory' => true,
      ],
      \$config = \Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/Entity'], true),
    );
    \$entityManager = new \Doctrine\ORM\EntityManager(\$connection, \$config);
    (new \Doctrine\ORM\Tools\SchemaTool(\$entityManager))->createSchema(\$entityManager->getMetadataFactory()->getAllMetadata());
    \$article = \$entityManager->find(App\Entity\Test::class, ['id' => [1234]]); // <-------- throws the warning we want
    " > reproduce.php
    
  • Run the PHP script reproduce.php
    php reproduce.php
    
    You should get a PHP Warning: Array to string conversion

Expected behavior

It would be nice to get just an UnexpectedValueException explaining the problem.

Originally created by @symfonyaml on GitHub (Feb 7, 2024). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.18.x ### Summary Misuse of `$em->find(...)` with array as identifier. ### Current behavior ```php $em->find( Test::class, ['id' => [1234]], // <----- Note the identifier is an array, which is wrong ); // returns the following PHP warning : // PHP Warning: Array to string conversion in UnitOfWork.php on line 1560 ``` ### How to reproduce - Create a new directory ```bash mkdir ./reproduce-doctrine-bug cd ./reproduce-doctrine-bug ``` - Install the `doctrine/orm` and `symfony/cache` repos ```bash composer require doctrine/orm:^2.18 symfony/cache ``` - Create a Test entity ```bash mkdir Entity echo "<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Test { #[ORM\Column(name: 'id', type: 'integer')] #[ORM\Id] #[ORM\GeneratedValue(strategy: 'AUTO')] private \$id; } " > Entity/Test.php ``` - Create the PHP script `test.php` to throw the error ```bash echo "<?php // reproduce.php require_once 'vendor/autoload.php'; require_once 'Entity/Test.php'; \$connection = \Doctrine\DBAL\DriverManager::getConnection( [ 'driver' => 'pdo_sqlite', 'path' => tempnam(sys_get_temp_dir(), 'doctrine') . '.sqlite', 'memory' => true, ], \$config = \Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/Entity'], true), ); \$entityManager = new \Doctrine\ORM\EntityManager(\$connection, \$config); (new \Doctrine\ORM\Tools\SchemaTool(\$entityManager))->createSchema(\$entityManager->getMetadataFactory()->getAllMetadata()); \$article = \$entityManager->find(App\Entity\Test::class, ['id' => [1234]]); // <-------- throws the warning we want " > reproduce.php ``` - Run the PHP script `reproduce.php` ``` php reproduce.php ``` You should get a `PHP Warning: Array to string conversion` ### Expected behavior It would be nice to get just an `UnexpectedValueException` explaining the problem.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7312