Symfony 6集成Gedmo Loggable非Mongo环境报错:如何无Mongo使用?
问题解决:Symfony 6 + MSSQL环境下使用Gedmo Loggable(无需MongoDB)
明确结论:Gedmo Loggable完全支持Doctrine ORM(包括MSSQL),不需要依赖MongoDB,你遇到的报错是版本更新后的配置错误导致的。
问题原因
Gedmo 3.x版本后,LogEntryRepository默认做了ORM/ODM兼容处理,但如果项目仅使用ORM,未手动指定ORM专属仓库类时,会触发MongoDB相关类的加载逻辑,导致找不到DocumentRepository的报错。
修复步骤
1. 移除冗余的ORM映射配置
你手动添加的gedmo_loggable映射配置是多余的,StofDoctrineExtensionsBundle已自动处理Gedmo的映射逻辑,直接删除这段配置:
# 从你的MSSQL ORM配置中删除以下内容 gedmo_loggable: type: annotation prefix: Gedmo\Loggable dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Loggable" alias: GedmoLoggable is_bundle: false
2. 配置StofDoctrineExtensionsBundle启用Loggable
在config/packages/stof_doctrine_extensions.yaml中添加Loggable启用配置,确保只开启ORM支持:
stof_doctrine_extensions: default_locale: zh_CN orm: default: loggable: true # 注意:不要添加mongodb相关配置
3. 指定ORM专属的LogEntry仓库类(核心修复)
创建自定义的LogEntry实体类,继承Gedmo的抽象类并指定ORM版本的仓库:
// src/Entity/LogEntry.php namespace App\Entity; use Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: \Gedmo\Loggable\Entity\Repository\LogEntryRepository::class)] class LogEntry extends AbstractLogEntry { // 无需额外代码,继承抽象类即可 }
这里的关键是指定ORM专属的
LogEntryRepository,而非默认的兼容类,避免触发MongoDB相关加载逻辑。
4. 重新生成并执行迁移
运行以下命令完成数据库迁移:
php bin/console make:migration php bin/console doctrine:migrations:migrate
版本变更说明
Gedmo 3.x之前的版本中,ORM和ODM的仓库类是完全分离的;3.x版本做了统一兼容封装,但如果项目仅使用ORM,必须手动指定ORM专属仓库类,否则会触发兼容逻辑中MongoDB类的加载,导致报错。
内容的提问来源于stack exchange,提问作者Ari Veiene
相关产品推荐
相关产品推荐

