a2lix / AutoFormBundle

Automate Symfony form building
https://a2lix.fr/bundles/auto-form
MIT License
83 stars 30 forks source link

Fixing associations in owning side #13

Closed gonzalezlopezjm closed 2 years ago

gonzalezlopezjm commented 5 years ago

I want to resolve #10 with this code

webda2l commented 5 years ago

Hi, I don't have good results on manual testing this code with my typical use case https://github.com/a2lix/Demo. It need deeper work with a bunch of phpunit tests. Feel free to work on it! In my case, I don't plan to do work on it in near future.

tifabien commented 4 years ago

@gonzalezlopezjm, I just tested your fix in the Demo repo and indeed I also faced issues.

If you're interested, I found a workaround in order to display *-to-one fields. I added another preSetData eventListener to my form and added manually the associated field to my TranslationType. It's ugly but in my case it works...

Here is the sample code :

$form->getFormBuilder()
   ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
        $this->onPreSetData($event);
    })
 ;

private function onPreSetData(FormEvent $event): void
{
    $formTranslations = $event->getForm()->get('translations');
    $options = $formTranslations->getConfig()->getOptions();
    foreach ($options['locales'] as $locale) {
        $formTranslations->add($locale, AutoFormType::class, [
            'data_class' => GalleryTranslation::class,
        ]);

        $formTranslations->get($locale)->add('gallery', EntityType::class, [
            'label' => false,
            'class' => Gallery::class,
            'required' => false,
        ]);
}
tontof commented 4 years ago

I had same problem with a OneToOne relation and I modify getFieldsConfig in src/ObjectInfo/DoctrineORMInfo.php. I replace

if (!empty($fields = $metadata->getFieldNames())) {
  $fieldsConfig = array_fill_keys($fields, []);
}

with

if (!empty($fields = array_diff(
  array_keys($metadata->getReflectionProperties()),
  $metadata->getAssociationNames()))
) {
  $fieldsConfig = array_fill_keys($fields, []);
}

It's OK with all tests. I'm wondering if it's linked to your problem. [This does not work as expected]

jordisala1991 commented 2 years ago

I think this one is also solved.

webda2l commented 2 years ago

I think this one is also solved.

Indeed. I close.