我正在尝试根据文件夹结构自动加载实体映射。我的加载器类返回一个像下面这个数组一样的地图。我的目标是在应用程序初始化时找到所有功能并将其域文件夹映射到学说 ORM 实体映射。
[
"UploadsDomain" => [
"alias" => "SomeNameDomain"
"dir" => "/home/someName/projects/video-service/src/Capability/SomeName/Domain"
"is_bundle" => false
"prefix" => "App\Capability\SomeName\Domain"
"type" => "attribute"
]
]
我的编译器传递是这样的,我在 Kernel.php 中调用它
$container->addCompilerPass(new DoctrineMappingPass());
。
namespace App\DependencyInjection\Compiler;
use App\Doctrine\CapabilityMappingLoader;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class DoctrineMappingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('doctrine.orm.default_configuration')) {
return;
}
$definition = $container->getDefinition('doctrine.orm.default_configuration');
// Instantiate the mapping loader
$loader = new CapabilityMappingLoader();
$mappings = $loader->loadMappings();
foreach ($mappings as $alias => $mapping) {
$definition->addMethodCall('addEntityNamespace', [$alias, $mapping['prefix']]);
$definition->addMethodCall('setMetadataDriverImpl', [
new AttributeDriver([$mapping['dir']]),
]);
}
}
}
但是,由于某种原因,我收到此错误消息:
Symfony\Component\DependencyInjection\Exception\RuntimeException:如果参数是对象或资源,则无法转储服务容器,得到“Doctrine\ORM\Mapping\Driver\AttributeDriver”。
知道如何让它发挥作用吗?
您正在将
AttributeDriver
的具体实例传递给 addMethodCall
。
尝试为每个所需的
AttributeDriver
实例创建服务定义。
目前无法建立一个项目来测试这一点,但我认为这应该让你继续前进,或者至少朝着正确的方向前进:
class DoctrineMappingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('doctrine.orm.default_configuration')) {
return;
}
$definition = $container->getDefinition('doctrine.orm.default_configuration');
// Instantiate the mapping loader
$loader = new CapabilityMappingLoader();
$mappings = $loader->loadMappings();
foreach ($mappings as $alias => $mapping) {
$attributeDriverId = $alias . '_' . mapping['alias'];
// Define the service for AttributeDriver
$container->register($attributeDriverId, AttributeDriver::class)
->addArgument([$mapping['dir']]);
$definition->addMethodCall('addEntityNamespace', [$alias, $mapping['prefix']]);
$definition->addMethodCall('setMetadataDriverImpl', [
new Reference($attributeDriverId),
]);
}
}
}