Skip to content
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

Open
cacahouwete opened this issue Aug 21, 2019 · 3 comments
Open

Comments

@cacahouwete
Copy link

The service "a2lix_auto_form.manipulator.default" has a dependency on a non-existent service "doctrine.orm.default_entity_manager".

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 ?

@h3llr4iser
Copy link

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?

@LouWii
Copy link

LouWii commented Feb 25, 2022

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.

@h3llr4iser
Copy link

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'
      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants