ill-logical
ill-logical

Reputation: 165

How to store intermediary entities via embedded forms

I want users to fill out a single form, but then create and store multiple Doctrine entities - a Company entity and a related User entity.

I have read about embedded forms and how I could embed a "sub-form" into my main form for the related entity.

However, my two entities are related via an intermediary entity Team Membership - an entity built above the join table companies_users. This entity is necessary, because I track some extra data concerning each relationship.

I did not manage to find any documentation on this. Is it possible or even practical to use embedded forms in this case? Is there some way to define that the embedded form for the target related entity (User) should also create an intermediary entity?

I would like my form to have a data class, because I like Validator annotations and setting up validation rules directly on the form class does not seem robust enough, and seems quite inelegant.

Thank you for your answers.

Upvotes: 2

Views: 84

Answers (1)

netsuo
netsuo

Reputation: 109

You could create the intermediate entity's form even if it doesn't contain any field. I actually use this in some of my projects, something like this:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('team_membership', TeammembershipType::class, [
                'label' => false
            ]);
    }

and then in TeammembershipType:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('user', UserType::class, [
                'label' => false
            ]);
    }

You could also set the team membership data in the intermediate form's events.

Upvotes: 2

Related Questions