Symfony 6.2.1:使用自定义实体管理器创建实体失败
问题描述
环境:Symfony 6.2.1、PHP 8.1.2
已配置多实体管理器,doctrine.yaml内容如下:
doctrine: dbal: default_connection: default connections: default: # configure these for your database server url: '%env(resolve:DATABASE_URL)%' driver: 'pdo_mysql' server_version: '5.7' charset: utf8mb4 customer: # configure these for your database server url: '%env(resolve:DATABASE_CUSTOMER_URL)%' driver: 'pdo_mysql' server_version: '5.7' charset: utf8mb4 orm: default_entity_manager: default entity_managers: default: connection: default mappings: Main: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity/Main' prefix: 'App\Entity\Main' alias: Main customer: connection: customer mappings: Customer: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity/Customer' prefix: 'App\Entity\Customer' alias: Customer
尝试生成第二个实体管理器对应的实体时失败,使用过以下命令:
php bin/console make:entity customer php bin/console make:entity Customer php bin/console make:entity 'App\Entity\Customer' php bin/console make:entity App\\Entity\\Customer
均得到错误提示:
Cannot find the entity manager for class "App\Entity\Customer"
解决方案
你的配置本身没有问题,错误出在make:entity命令的用法上。要指定使用哪个实体管理器生成实体,需要添加--manager参数,而非把管理器名称或实体命名空间直接作为命令参数。
正确的命令格式为:
php bin/console make:entity --manager=customer
执行该命令后,会提示你输入实体名称(比如User),生成的实体类会自动放在src/Entity/Customer目录下,对应App\Entity\Customer命名空间,同时关联到customer实体管理器。
若要为App\Entity\Customer下已存在的实体添加字段,需确保实体类命名空间正确,同时指定--manager=customer参数:
php bin/console make:entity --manager=customer [实体名称]
内容的提问来源于stack exchange,提问作者Juergen Schulze
相关产品推荐
相关产品推荐

