Home > Software design >  How can I relation a foreign key with a form in symfony?
How can I relation a foreign key with a form in symfony?

Time:01-27

Hi I'm trying to make a relation between two entities, a user creates proyectes, but in the create a project form I don't know how do it.

If I do this:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
          ->add('Nombre')
          ->add('Resumen')
          ->add('Creados')
      ;
}

I have the following error:

error img

What can i do to make the form that make a relation and show the user how creates the proyect? It's my first time using stakoverflow, thank you!

CodePudding user response:

you could use the EntityType for a to-one relationship like:

$builder->add('users', EntityType::class, [
   'class' => User::class,

   // uses the User.username property as the visible option string
   'choice_label' => 'username',

]);

Have a look at the documentation, too. This will help you understand other form field types as well.

For a to-many relationship you can use Collection which is a bit more complicated.

  • Related