EntityRepository::findBy() no longer works with 'id' key #6866

Open
opened 2026-01-22 15:40:21 +01:00 by admin · 2 comments
Owner

Originally created by @dsentker on GitHub (Oct 22, 2021).

Bug Report

Q A
BC Break idk
Version 2.10.2

Summary

Before ORM < 2.10.2, i was able to collect all entities by ID with the Doctrine\ORM\EntityRepository::findBy method:

$ids = [1, 2, 3];
return $this->findBy(['id' => $ids]);

This no longer works.

Current behavior

An exception has been thrown during the rendering of a template ("An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty").

I initially thought that my array ($ids) was empty, but this was not the case.

How to reproduce

With given Version, try to obtain entities by id:

// inside a repository:
public function getEntitiesById(array $ids): array
{
    return $this->findBy(['id' => $ids]);
}

Expected behavior

The repository returns an array of Entities matching the ID provided in the findBy Method.

However, a workaround is to use a new query builder instance and use the clumsy DQL IN(...) command:

return $this->createQueryBuilder('a')
            ->andWhere('a.id IN (:ids)')
            ->setParameters([
                'ids' => $ids,
            ])
            ->getQuery()
            ->getArrayResult();

Update1:
I found out that the findBy() method call is not responsible to reproduce this issue. I got the error described before with the the QueryBuilder too (but strangely, only in some cases).

I was able to omit the error when i do not pass an array of ids. When i convert the array to a string, everything works fine:

$qb = $this->createQueryBuilder('a')
            ->andWhere('a.id IN (:ids)')
            ->setParameters([
                'ids' => implode(',', $ids), // <--- note my glue here
            ])
        ;

Update 2:
I tried passing a new ArrayCollection as stated in the documentation, but this will not work either:

$qb = $this->createQueryBuilder('a')
            ->andWhere('a.id IN (:ids)')
            ->setParameters(new ArrayCollection([
               new Query\Parameter('ids', $ids)
            ]))
        ;
// Syntax error or access violation: 1065 Query was empty").

Update 3:
Using the Expr class seems to solve the problem:

$qb->->andWhere($qb->expr()->in('a.id', $ids));
Originally created by @dsentker on GitHub (Oct 22, 2021). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | idk | Version | 2.10.2 #### Summary Before ORM < 2.10.2, i was able to collect all entities by ID with the Doctrine\ORM\EntityRepository::findBy method: ```php $ids = [1, 2, 3]; return $this->findBy(['id' => $ids]); ``` This no longer works. #### Current behavior > An exception has been thrown during the rendering of a template ("An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty"). I initially thought that my array ($ids) was empty, but this was not the case. #### How to reproduce With given Version, try to obtain entities by id: ```php // inside a repository: public function getEntitiesById(array $ids): array { return $this->findBy(['id' => $ids]); } ``` #### Expected behavior The repository returns an array of Entities matching the ID provided in the findBy Method. ~~However, a workaround is to use a new query builder instance and use the clumsy DQL IN(...) command:~~ ```php return $this->createQueryBuilder('a') ->andWhere('a.id IN (:ids)') ->setParameters([ 'ids' => $ids, ]) ->getQuery() ->getArrayResult(); ``` **Update1:** I found out that the findBy() method call is not responsible to reproduce this issue. I got the error described before with the the QueryBuilder too (but strangely, only in some cases). I was able to omit the error when i do not pass an array of ids. When i convert the array to a string, everything works fine: ```php $qb = $this->createQueryBuilder('a') ->andWhere('a.id IN (:ids)') ->setParameters([ 'ids' => implode(',', $ids), // <--- note my glue here ]) ; ``` **Update 2:** I tried passing a new ArrayCollection [as stated in the documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/query-builder.html#the-querybuilder), but this will not work either: ```php $qb = $this->createQueryBuilder('a') ->andWhere('a.id IN (:ids)') ->setParameters(new ArrayCollection([ new Query\Parameter('ids', $ids) ])) ; // Syntax error or access violation: 1065 Query was empty"). ``` **Update 3:** Using the `Expr` class seems to solve the problem: ```php $qb->->andWhere($qb->expr()->in('a.id', $ids)); ```
admin added the BugBC Break labels 2026-01-22 15:40:21 +01:00
Author
Owner

@greg0ire commented on GitHub (Oct 23, 2021):

Possible next steps:

  • find the commit by using git bisect
  • write a PHPUnit test that passes on 2.10.1, but fails on 2.10.2
@greg0ire commented on GitHub (Oct 23, 2021): Possible next steps: - find the commit by using `git bisect` - write a PHPUnit test that passes on 2.10.1, but fails on 2.10.2
Author
Owner

@sir-kain commented on GitHub (Feb 5, 2022):

I wrote this following assertion and it fails even on 2.10.0. For commit 011d3c21eb

<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

use function assert;

/**
 * @group GH-9144
 */
class GH9144Test extends OrmFunctionalTestCase
{
  protected function setUp(): void
  {
    parent::setUp();
    try {
      $this->_schemaTool->createSchema(
        [
          $this->_em->getClassMetadata(GH9144Author::class),
        ]
      );

      // Create author 11/Joe
      $author           = new GH9144Author();
      $author->id       = 11;
      $author->username = 'Joe';
      $this->_em->persist($author);

      // Create author 12/Sir
      $author1           = new GH9144Author();
      $author1->id       = 12;
      $author1->username = 'Sir';
      $this->_em->persist($author1);

      $this->_em->flush();
      $this->_em->clear();
    } catch (Exception $e) {
    }
  }

  public function testFindByWithIds(): void
  {
    $ids = [11, 12];
    $authors = $this->_em->getRepository(GH9144Author::class)->findBy(
      ['id' => $ids]
    );

    self::assertCount(2, $authors);
  }
}

/**
 * @Entity
 * @Table(name="gh9144_author")
 */
class GH9144Author
{
  /**
   * @var int
   * @Id
   * @Column(type="integer")
   * @GeneratedValue
   */
  public $id;

  /**
   * @var string
   * @Column(type="string", length=255, unique=true)
   */
  public $username;
}

@sir-kain commented on GitHub (Feb 5, 2022): I wrote this following assertion and it fails even on `2.10.0`. For commit 011d3c21eb203cb2a93edaf9df7254fb927907c5 ```php <?php declare(strict_types=1); namespace Doctrine\Tests\ORM\Functional\Ticket; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\Table; use Doctrine\Tests\OrmFunctionalTestCase; use Exception; use function assert; /** * @group GH-9144 */ class GH9144Test extends OrmFunctionalTestCase { protected function setUp(): void { parent::setUp(); try { $this->_schemaTool->createSchema( [ $this->_em->getClassMetadata(GH9144Author::class), ] ); // Create author 11/Joe $author = new GH9144Author(); $author->id = 11; $author->username = 'Joe'; $this->_em->persist($author); // Create author 12/Sir $author1 = new GH9144Author(); $author1->id = 12; $author1->username = 'Sir'; $this->_em->persist($author1); $this->_em->flush(); $this->_em->clear(); } catch (Exception $e) { } } public function testFindByWithIds(): void { $ids = [11, 12]; $authors = $this->_em->getRepository(GH9144Author::class)->findBy( ['id' => $ids] ); self::assertCount(2, $authors); } } /** * @Entity * @Table(name="gh9144_author") */ class GH9144Author { /** * @var int * @Id * @Column(type="integer") * @GeneratedValue */ public $id; /** * @var string * @Column(type="string", length=255, unique=true) */ public $username; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6866