Doctrine短命名空间别名不兼容问题及替代方案查询
Doctrine Persistence 3.x 不支持短命名空间别名的解决方案
问题背景
在Symfony 6.4项目开发CLI功能时,调用EntityManager::getRepository使用短命名空间别名App:SampleNature触发报错,提示该特性在doctrine/persistence 2.x已废弃,3.x版本不再支持。
报错信息
In NotSupported.php line 31: Context: Using short namespace alias "App:SampleNature" when calling Doctrine\ORM\EntityManager::getRepository Problem: Feature was deprecated in doctrine/persistence 2.x and is not supported by installed doctrine/persistence:3.x Solution: See the doctrine/deprecations logs for new alternative approaches.
相关代码片段
<?php namespace App\Command; use App\Entity\SampleNature; … use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; … #[AsCommand(name: 'app:migrate-samples-results')] class MigrateResultsCommand extends Command { private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; parent::__construct(); } public function getCursorResult(SymfonyStyle $terminal, $fp_recept, $fp_result) { … // 触发问题的代码行 $nature = $this->entityManager->getRepository('App:SampleNature')->findOneBy($nature_ech); … } … }
当前项目依赖版本
composer show -i | grep doctrine You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages. doctrine/annotations 2.0.1 Docblock Annotations Parser doctrine/cache 2.2.0 PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others. doctrine/collections 2.2.2 PHP Doctrine Collections library that adds additional functionality on top of PHP arrays. doctrine/common 3.4.4 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, proxie... doctrine/dbal 3.9.0 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management. doctrine/deprecations 1.1.3 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages. doctrine/doctrine-bundle 2.12.0 Symfony DoctrineBundle doctrine/doctrine-migrations-bundle 3.3.1 Symfony DoctrineMigrationsBundle doctrine/event-manager 2.0.1 The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects. doctrine/inflector 2.0.10 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words. doctrine/instantiator 2.0.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer 3.0.1 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers. doctrine/migrations 3.8.0 PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily de... doctrine/orm 2.19.6 Object-Relational-Mapper for PHP doctrine/persistence 3.3.3 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. doctrine/sql-formatter 1.4.1 a PHP SQL highlighting library symfony/doctrine-bridge v6.4.10 Provides integration for Doctrine with various Symfony components symfony/doctrine-messenger v6.4.9 Symfony Doctrine Messenger Bridge
解决方案
替代方案1:使用实体类的完全限定类名
代码中已经引入了App\Entity\SampleNature,直接将短别名替换为类名即可:
// 替换前 $nature = $this->entityManager->getRepository('App:SampleNature')->findOneBy($nature_ech); // 替换后 $nature = $this->entityManager->getRepository(SampleNature::class)->findOneBy($nature_ech);
替代方案2:直接注入实体Repository
Symfony中更推荐的做法是直接注入对应实体的Repository类,而非通过EntityManager获取:
- 确保实体的Repository类存在(Doctrine默认自动生成,也可自定义)
- 修改Command的构造函数,注入
SampleNatureRepository:
use App\Repository\SampleNatureRepository; #[AsCommand(name: 'app:migrate-samples-results')] class MigrateResultsCommand extends Command { private $sampleNatureRepository; public function __construct(SampleNatureRepository $sampleNatureRepository) { $this->sampleNatureRepository = $sampleNatureRepository; parent::__construct(); } public function getCursorResult(SymfonyStyle $terminal, $fp_recept, $fp_result) { … $nature = $this->sampleNatureRepository->findOneBy($nature_ech); … } }
版本兼容说明
doctrine/orm 2.19.x 本身兼容 doctrine/persistence 3.x,这是正常的依赖组合,无需困惑。废弃短命名空间别名是persistence 3.x的明确变更,目的是统一使用完全限定类名获取Repository。
查看废弃日志的方法
无需跳转外部网站,可通过两种方式获取详细替代方案:
- 查看项目
vendor/doctrine/deprecations目录下的日志文件 - 执行CLI命令时添加
-vvv参数,控制台会输出完整的废弃提示及替代说明
内容的提问来源于stack exchange,提问作者bcag2
相关产品推荐
相关产品推荐

