Open cacahouwete opened 5 years ago
I just update TranslationFormBundle to branch 3 and I have the similar problem. I have a default entity manager but I need to use in some forms a customer entity manager. Need to specify the entity manager for each form. Have you managed to solve the problem?
Overriding the definition to pass your own entity manager is fairly easy. I've put that into my services.yaml
config file for the app.
# Override a2lix service definition to use the core entity manager
a2lix_auto_form.doctrine.metadata_factory:
class: Doctrine\Persistence\Mapping\ClassMetadataFactory
factory: ['@doctrine.orm.my_custom_entity_manager', 'getMetadataFactory']
The problem is when you have multiple entity managers, and need forms that are spread across multiple of them, I'm still trying to figure out how to handle that. Seems impossible as it is.
DoctrineORMInfo
is where the problem starts. It can only handle 1 ClassMetadataFactory
. But really, it should be setup so it can handle all entity managers that exist in the app, and find which one to use depending on the entity class. Should be doable.
I've been able to come up with a workaround by decorating the service in the following manner:
<?php
namespace App\AutoForm;
use A2lix\AutoFormBundle\Form\Type\AutoFormType;
use A2lix\AutoFormBundle\ObjectInfo\DoctrineORMInfo;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class DoctrineORMInfoDecorator extends DoctrineORMInfo
{
public function __construct(private DoctrineORMInfo $doctrineORMInfo, private ManagerRegistry $managerRegistry)
{
}
public function getFieldsConfig(string $class): array
{
$fieldsConfig = [];
$em = $this->managerRegistry->getManagerForClass($class);
$classMetadataFactory = $em->getMetadataFactory();
$metadata = $classMetadataFactory->getMetadataFor($class);
if (!empty($fields = $metadata->getFieldNames())) {
$fieldsConfig = array_fill_keys($fields, []);
}
if (!empty($assocNames = $metadata->getAssociationNames())) {
$fieldsConfig += $this->getAssocsConfig($metadata, $assocNames);
}
return $fieldsConfig;
}
public function getAssociationTargetClass(string $class, string $fieldName): string
{
$em = $this->managerRegistry->getManagerForClass($class);
$classMetadataFactory = $em->getMetadataFactory();
$metadata = $classMetadataFactory->getMetadataFor($class);
if (!$metadata->hasAssociation($fieldName)) {
throw new \RuntimeException(sprintf('Unable to find the association target class of "%s" in %s.', $fieldName, $class));
}
return $metadata->getAssociationTargetClass($fieldName);
}
private function getAssocsConfig(ClassMetadata $metadata, array $assocNames): array
{
$assocsConfigs = [];
foreach ($assocNames as $assocName) {
$associationMapping = $metadata->getAssociationMapping($assocName);
if (isset($associationMapping['inversedBy'])) {
$assocsConfigs[$assocName] = [];
continue;
}
$class = $metadata->getAssociationTargetClass($assocName);
if ($metadata->isSingleValuedAssociation($assocName)) {
$assocsConfigs[$assocName] = [
'field_type' => AutoFormType::class,
'data_class' => $class,
'required' => false,
];
continue;
}
$assocsConfigs[$assocName] = [
'field_type' => CollectionType::class,
'entry_type' => AutoFormType::class,
'entry_options' => [
'data_class' => $class,
],
'allow_add' => true,
'by_reference' => false,
];
}
return $assocsConfigs;
}
}
App\AutoForm\DoctrineORMInfoDecorator:
decorates: a2lix_auto_form.object_info.doctrine_orm_info
arguments:
$doctrineORMInfo: '@.inner'
$managerRegistry: '@doctrine'
In my case I have multiple doctrine connections and then entity manager. My default entity manager service name is not default but something like "commonDb". Is it possible to add an option configuration to say witch one we want to target ?