#698·orm

`getManagerForClass()` returns wrong manager since 3.3.2 when mapping is scoped by paths only

Author: bravikCreated May 29, 2026Updated Jun 3, 2026

Since PR #693 (shipped in 3.3.2), IlluminateRegistry::getManagerForClass() returns the first manager in config order for every mapped class, regardless of whether that manager's configured paths actually map the class. The regression persists in 3.3.3.

This breaks multi-manager configurations where:

  • multiple managers use the attribute driver (the default), and
  • entity sets are scoped to managers via the paths config key only — i.e. without registering driver namespaces in a MappingDriverChain.

That configuration shape is the one the package's own default EntityManagerFactory::setCustomMappingDriverChain() produces, so any consumer following the recommended setup is exposed.

Repro

config/doctrine.php:

php
return [
    'managers' => [
        'admin' => array_merge($common, [
            'connection' => 'admin',
            'paths'      => [base_path('app/Admin/Entities')],
        ]),
        'tenant' => array_merge($common, [
            'connection' => 'tenant',
            'paths'      => [base_path('app/Tenant/Entities')],
        ]),
    ],
];
  • App\Admin\Entities\AdminThing — #[ORM\Entity], lives only under app/Admin/Entities.
  • App\Tenant\Entities\TenantThing — #[ORM\Entity], lives only under app/Tenant/Entities.

Then:

php
$registry = app(\Doctrine\Persistence\ManagerRegistry::class);
$registry->getManagerForClass(\App\Tenant\Entities\TenantThing::class);
version result
3.3.1 tenant manager ✅
3.3.2 admin manager ❌
3.3.3 admin manager ❌

A persistence operation now hits the admin connection instead of the tenant connection, typically surfacing as a TableNotFoundException against a table that exists in the other database.

Root cause

Before #693, the loop in IlluminateRegistry::getManagerForClass() had two filters: a cheap isTransient() short-circuit and a per-manager getAllMetadata() exact-name check. The second filter is what made path-only scoping work.

After #693:

php
foreach ($this->getManagers() as $entityManager) {
    if (! $entityManager->getMetadataFactory()->isTransient($className)) {
        return $entityManager;
    }
}

The correctness of this implementation requires MetadataFactory::isTransient($class) to return true for any class the manager does not map. That holds for XmlDriver / YamlDriver (they check for a mapping file under the configured paths), and for MappingDriverChain::isTransient() when drivers are registered under their real namespace prefixes via addDriver($driver, $namespace).

It does not hold for AttributeDriver (and the legacy AnnotationDriver). Their isTransient($class) is a reflection-only check for the presence of #[Entity] / #[MappedSuperclass] on the class. It ignores the manager's configured paths entirely, so every manager that loads an attribute driver returns the same answer (false) for any class tagged #[Entity] — regardless of whether that class is reachable from its paths.

In the package's default setup, EntityManagerFactory::setCustomMappingDriverChain() wraps the attribute driver in a MappingDriverChain whose only registered driver is bound to the literal prefix 'LaravelDoctrine':

php
// vendor/laravel-doctrine/orm/src/EntityManagerFactory.php
$chain = new MappingDriverChain(
    $configuration->getMetadataDriverImpl(),
    'LaravelDoctrine',
);

Application entities are not under LaravelDoctrine\…, so the prefix never matches and every isTransient call falls through to the default driver — which is the path-blind attribute driver. The pre-#693 getAllMetadata() check masked this; #693 removed the mask.

Why the pre-#693 behavior worked

getAllMetadata() is path-aware: each manager's metadata factory only discovers classes its configured paths reach. So the inner loop in 3.3.1 acted as the actual path-scoping check that isTransient() was supposed to be.

The performance win in #693 is real, but the cheaper signal it relies on is not equivalent for the most common configuration shape this package ships.

Suggested fixes

Preferred — make isTransient actually path/namespace-aware by default

Have EntityManagerFactory::setCustomMappingDriverChain() register the attribute driver under the application's real namespaces instead of the literal 'LaravelDoctrine' prefix. The package already accepts a namespaces key per manager (it appears in user configs in the wild) — wiring this to MappingDriverChain::addDriver() and dropping the default driver fallthrough would fix isTransient per the Doctrine contract and make #693 correct.

Rough shape:

php
protected function setCustomMappingDriverChain(array $settings, Configuration $configuration): void
{
    $driver     = $configuration->getMetadataDriverImpl();
    $namespaces = Arr::get($settings, 'namespaces', []);

    $chain = new MappingDriverChain();
    foreach ($namespaces as $namespace) {
        $chain->addDriver($driver, $namespace);
    }
    // No setDefaultDriver(): unknown classes report isTransient = true.

    $configuration->setMetadataDriverImpl($chain);
}

This matches how Symfony's DoctrineBundle configures multi-manager setups and aligns with Doctrine's documented contract for MappingDriverChain.

Fallback — restore the inner getAllMetadata check, but memoize

If a namespaces requirement is too invasive, the alternative is to keep isTransient() as a cheap pre-filter but still confirm via a one-time, cached metadata sweep:

php
foreach ($this->getManagers() as $manager) {
    if ($manager->getMetadataFactory()->isTransient($className)) {
        continue;
    }
    foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
        if ($metadata->getName() === $className) {
            return $manager;
        }
    }
}

getMetadataFactory()->getAllMetadata() caches internally after the first call, so the amortized cost matches the new behavior on hot paths while preserving correctness for path-only scoping.

Environment

  • laravel-doctrine/orm: 3.3.2 and 3.3.3 (regression introduced in 3.3.2)
  • doctrine/orm: 3.x
  • doctrine/persistence: provides the MappingDriverChain whose isTransient semantics are described above.
  • PHP 8.4, Laravel 12.

Workaround we shipped

We applied the "preferred" fix above in userland: subclass EntityManagerFactory, override setCustomMappingDriverChain() to register the attribute driver under each declared application namespace and drop the default-driver fallthrough, then bind the subclass in a service provider. The upstream getManagerForClass() then routes correctly on 3.3.3 — no ManagerRegistry decorator required.

php
final class NamespaceAwareEntityManagerFactory extends EntityManagerFactory
{
    protected function setCustomMappingDriverChain(array $settings, Configuration $configuration): void
    {
        $driver = $configuration->getMetadataDriverImpl();
        Assert::isInstanceOf($driver, MappingDriver::class);

        $namespaces = Arr::get($settings, 'namespaces', []);
        Assert::isArray($namespaces);
        Assert::allString($namespaces);
        Assert::notEmpty($namespaces, 'Each manager must declare `namespaces` so isTransient() routes correctly.');

        $paths = Arr::get($settings, 'paths', []);
        if ($paths !== [] && method_exists($driver, 'addPaths')) {
            $driver->addPaths($paths);
        }

        $chain = new MappingDriverChain();
        foreach ($namespaces as $namespace) {
            $chain->addDriver($driver, $namespace);
        }
        $configuration->setMetadataDriverImpl($chain);
    }

    protected function registerPaths(array $settings, Configuration $configuration): void
    {
        // No-op: paths already attached to the inner driver above so that
        // getAllClassNames() reflects per-manager scope while the chain
        // owns isTransient().
    }
}

Bind it in a service provider registered after DoctrineServiceProvider:

php
$this->app->singleton(EntityManagerFactory::class, NamespaceAwareEntityManagerFactory::class);

Other workaround options if subclassing isn't viable: pin to 3.3.1 (blocks later 3.x patches), or decorate ManagerRegistry and override getManagerForClass() with the pre-#693 two-filter implementation (carries the same correctness assumption forward).