Create nested Subentities in Forms

Viewed 56

My Entity Tree looks like this:

basicpage
|_ contentModule  (parent)
    |_ textModule  (child)
    |_ mediaModule (child)
         |_ mediaElement

I have a Basic Page Entity which has a collection of content modules. Now I want to create multiple childrens of contentModules within the basicpage form. To manage these childrens I try to use doctrine and inheritence mapping. I try the following:

Basicpage Entity

 class Basicpage 
 {
 #[ORM\OneToMany(mappedBy: 'basicpage', targetEntity: ContentModule::class, cascade:["persist"])]
 private $contentModules;

ContentModule (parent entity)

 #[ORM\Entity(repositoryClass: ContentModuleRepository::class)]
 #[ORM\InheritanceType("JOINED")]
 #[ORM\DiscriminatorColumn(name: "disc", type: "string")]
 #[ORM\DiscriminatorMap(["textModule" => TextModule::class,"mediaModule" => MediaModule::class])]

 class ContentModule
 {
 #[ORM\Id]
 #[ORM\GeneratedValue]
 #[ORM\Column(type: 'integer')]
 protected $id;

 #[ORM\ManyToOne(targetEntity: Basicpage::class, inversedBy: 'contentModules')]
 private $basicpage;

TextModule (child entity)

class TextModule extends ContentModule 
{

private $type = 'textModule';

#[ORM\Column(type: 'string', length: 6000)]
private $text;

MediaModule (child entity)

 class MediaModule extends ContentModule
 {

  private $type = 'mediaModule';

  #[ORM\ManyToMany(targetEntity: MediaElement::class, inversedBy: 'mediaModules', cascade:["persist"])]
  private $mediaElement;

BasicpageType

class BasicpageType extends AbstractType
{
    $builder->add('contentModules', CollectionType::class, [
            'entry_type' => ContentModuleType::class,
            'entry_options' => ['label' => false],
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
        ]);

ContentModuleType

class ContentModuleType extends AbstractType
{
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) {
        $child = $event->getData();
        $form = $event->getForm();
        
        if ($child instanceof TextModule) {
            $form->add('text', TextareaType::class);
        }

        if ($child instanceof MediaModule) {
            $form->add('mediaElement', EntityType::class, array(
                'class' => 'App\Entity\ContentModule\MediaModule\MediaElement',
                'multiple' => true,
                'label' => 'Mediaelement: '));
        }

And finally the rendering

      <div class="sub" data-prototype="{{ form_widget(form.contentModules.vars.prototype)|e('html_attr') }}"
           data-entitytype="contentModule"> 

            {% for module in form.contentModules %}
                {{ form_row(module) }}
            {% endfor %}
      </div>

the problem is that i can render a prototype of a contentModule, but cant get any prototypes of the inherienced entities.

0 Answers
Related