You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开启enable_lazy_ghost_objects后Symfony+Doctrine类未找到错误求助

启用Doctrine Lazy Ghost Objects后出现类找不到错误

环境信息

  • Symfony 6.4
  • Doctrine/ORM 2.17

问题背景

收到弃用提示:

Since doctrine/doctrine-bundle 2.11: Not setting "enable_lazy_ghost_objects" to true is deprecated

于是在Doctrine配置文件中添加:

orm:
    enable_lazy_ghost_objects: true

问题现象

配置修改后,定时任务及命令运行时大量报错,示例错误:

In LazyObjectRegistry.php line 70:
  [ErrorException]
  Warning: Class "arreteCompte" not found

Exception trace:
  at /var/www/html/vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php:70
 Symfony\Component\VarExporter\Internal\LazyObjectRegistry::getClassResetters() at /var/www/html/vendor/symfony/var-exporter/LazyGhostTrait.php:52
 Proxies\__CG__\App\Entity\Bail::createLazyGhost() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php:471
 Proxies\__CG__\App\Entity\Bail::Doctrine\ORM\Proxy\{closure}() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php:212
 Doctrine\ORM\Proxy\ProxyFactory->getProxy() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3050
 Doctrine\ORM\UnitOfWork->createEntity() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:272
 Doctrine\ORM\Internal\Hydration\ObjectHydrator->getEntity() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:498
 Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateRowData() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:149
 Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateAllData() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:270
 Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:1225
 Doctrine\ORM\AbstractQuery->executeIgnoreQueryCache() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:1166

错误触发于以下循环:

$assuranceBaux = $this->assuranceBailRepository->findAssuranceBailNeeded();

foreach ($assuranceBaux as $assuranceBail) {
    ...
}

相关代码

仓库查询方法

public function findAssuranceBailNeeded()
{
$qb = $this->createQueryBuilder('ab');

    return $qb->select('ab')
        ->join('ab.typeAssurance', 'at')
        ->join(
            'ab.bail',
            'b',
            Join::WITH,
            $qb->expr()->orX(
                ...
            )
        )
        ->getQuery()
        ->getResult();
}

AssuranceBail实体

class AssuranceBail
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'SEQUENCE')]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\ManyToOne(targetEntity: Bail::class, inversedBy: 'assurances')]
    #[ORM\JoinColumn(nullable: false)]
    private $bail;

    ...
}

Bail实体

class Bail
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'SEQUENCE')]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\OneToOne(mappedBy: 'bail', targetEntity: ArreteCompte::class, cascade: ['persist', 'remove'])]
    #[Groups(['detailRestitDg', 'listDg', 'detailRestitDg', 'informationsDg'])]
    private ?ArreteCompte $arreteCompte;
}

Bail代理类片段

namespace Proxies\__CG__\App\Entity;

/**
 * DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE'S PROXY GENERATOR
 */
class Bail extends \App\Entity\Bail implements \Doctrine\ORM\Proxy\InternalProxy
{
    use \Symfony\Component\VarExporter\LazyGhostTrait {
        initializeLazyObject as __load;
        setLazyObjectAsInitialized as public __setInitialized;
        isLazyObjectInitialized as private;
        createLazyGhost as private;
        resetLazyObject as private;
    }

    private const LAZY_OBJECT_PROPERTY_SCOPES = [
        ...
        "\0".parent::class."\0".'arreteCompte' => [parent::class, 'arreteCompte', 'arreteCompte'],
        ...
    ];
}

即使在仓库中关联查询了ArreteCompte表,错误依然存在,恳请帮忙排查问题。


内容的提问来源于stack exchange,提问作者pop_up

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 05:14:52