-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option configuration to target custom default entity manager #16
Comments
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. |
Overriding the definition to pass your own entity manager is fairly easy. I've put that into my
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.
|
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;
}
}
|
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 ?
The text was updated successfully, but these errors were encountered: