Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
symfonyaml opened this issue Feb 7, 2024 · 0 comments
Open

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

symfonyaml opened this issue Feb 7, 2024 · 0 comments

Comments

@symfonyaml
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant