Home > Back-end >  How can I relation a foreing_key with a form in symfony?
How can I relation a foreing_key with a form in symfony?

Time:01-26

Hi in trying to make a relation between two entitys, a user creates proyectes, but in the create a project form i dont 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:

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