Using EntityRepository::findBy() with Many-To-Many relations will lead to a E_NOTICE in Doctrine

Using EntityRepository::findBy() with Many-To-Many relations will lead to a E_NOTICE in Doctrine

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Using EntityRepository::findBy() with Many-To-Many relations will lead to a E_NOTICE in Doctrine

When working with Symfony2 and Doctrine ORM, you might encounter a subtle but frustrating issue when trying to query entities using EntityRepository::findBy() on a Many-to-Many relationship. Specifically, passing a collection of related objects (like Platforms) directly into the criteria array triggers an internal PHP E_NOTICE about an undefined index: joinColumns.

This behavior is particularly confusing because Doctrine doesn't throw a clear exception saying "this operation isn't supported." Instead, it silently generates a warning, often leading developers to assume the query should work but for some missing configuration. In reality, passing entity instances directly into findBy() criteria for Many-to-Many relationships is not how Doctrine expects you to query join tables.

Why the Error Occurs

The root cause lies in how Doctrine constructs the DQL for your query. When you call:

$posts = $postRepo->findBy(['platforms' => [$platform]]);

Doctrine attempts to translate this into a DQL statement that joins the map_post_platform join table. However, the internal logic in BasicEntityPersister.php expects certain metadata (like joinColumns) to be present when building the query plan for collections of associated entities. In many-to-many relationships defined with explicit join column specifications, Doctrine sometimes fails to populate this metadata correctly when used directly in a findBy() criteria array.

The error message:

ErrorException: Notice: Undefined index: joinColumns in .../BasicEntityPersister.php line 1495

is a symptom of Doctrine trying to access an array key that doesn't exist during query planning. It’s not a bug in your code, but rather a limitation in how the ORM handles complex association queries via findBy().

The Proper Way to Query Many-to-Many Relations

To avoid this issue and write reliable queries, you should use Doctrine's DQL directly or leverage repository methods that are specifically designed for association queries. Here's a working example using DQL:

$platform = $this->getEntityManager()->getRepository('FoobarCommunityBundle:Platform')->findOneBy(['id' => 1]);

$dql = 'SELECT p FROM FoobarBlogBundle:Post p
        JOIN p.platforms pl
        WHERE pl.id = :id';

$query = $this->getEntityManager()->createQuery($dql);
$query->setParameter('id', $platform->getId());

$posts = $query->getResult();

This approach gives you full control over the query logic and avoids any ambiguity that findBy() might introduce with many-to-many associations.

Repository Methods Are Also Limited

You may have noticed in your original code that even custom repository methods like findByPlatforms($platform) trigger the same issue if they internally rely on passing the entity instance into findBy(). That's because Doctrine still treats the criteria array in a similar way when constructing the query tree.

If you want to encapsulate this logic cleanly, consider implementing dedicated methods in your repository that build DQL explicitly:

// In PostRepository.php

public function findPostsByPlatform(Platform $platform) {
    return $this->createQueryBuilder('p')
        ->innerJoin('p.platforms', 'pl')
        ->where('pl.id = :id')
        ->setParameter('id', $platform->getId())
        ->getQuery()
        ->getResult();
}

This pattern keeps your business logic clean and avoids relying on undocumented behavior within Doctrine's query builder internals.

Version-Specific Behavior

It's worth noting that this issue is tied to older versions of Doctrine ORM, specifically those under version 2.4 (as indicated in your composer.json). Newer versions of Doctrine have improved their handling of association queries and may not exhibit this exact behavior. If you're on a supported Symfony LTS version with Doctrine 2.5 or later, consider upgrading both the ORM and the Bundle to see if the warning disappears.

Final Recommendation

While it might seem convenient to pass entities directly into findBy() for simplicity, especially when working with one-to-many relationships, many-to-many associations require more explicit handling. Using DQL or custom repository methods ensures your queries remain predictable and portable across Doctrine versions.

If you're maintaining legacy Symfony2 codebases, this is a known quirk worth documenting in team notes. For new projects, prefer explicit query building to avoid relying on ORM internals that may change or break unexpectedly.

Our network