Comments (3)
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.
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.
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)
- Fatal error in shared hosting HOT 1
- DoctrineORMInfo class unnecessary check? HOT 7
- AutoFormType doesn't support Embedded properties HOT 1
- Allow doctrine/persister:^2 HOT 1
- FormType and multiple translation entity inheritance HOT 1
- Can you please add a release to this repository? HOT 2
- having issues configuring a2lix
- Dead?
- Symfony 4(Symfony/Form 4.0) Integration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autoformbundle.