Giter VIP home page Giter VIP logo

Comments (3)

h3llr4iser avatar h3llr4iser commented on July 20, 2024

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?

from autoformbundle.

LouWii avatar LouWii commented on July 20, 2024

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.

from autoformbundle.

h3llr4iser avatar h3llr4iser commented on July 20, 2024

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'
      

from autoformbundle.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.