简体   繁体   中英

Form with one to many relation

In my database I have 2 tables:

Course table:

  • id (PK)
  • name (varchar)
  • description (text)

Session table:

  • id (PK)
  • date (datetime)
  • course_id (FK to id from course table)

The buildForm function in my SessionType looks like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('date');
}

The buildForm function in my CourseType looks like this:

$builder
    ->add('name')
    ->add('description')
    ->add('foreknowledge')
    ->add('teacher')
    ->add('sessions', CollectionType::class, array(
        'entry_type' => SessionType::class,
        'entry_options' => array('label' => false),
        'allow_add' => true,
        'by_reference' => false,
        'allow_delete' => true,
    ))

My form in my twig template looks like this:

{{ form_start(form) }}
    {{ form_widget(form) }}

    <input type="submit" value="Create" />
{{ form_end(form) }}

But nothing is shown under "Sessions". I would like to have the possibility to create multiple sessions under my course. Is there an easy way to do this?

UPDATE:

When I try to save my course and do a dump just before I save I get this:

在此处输入图片说明

My sessions are saved, but they are not linked to course. How does this come?

在此处输入图片说明

That is possible and you are missing some javascript, it is very well documented here .

Instead of copy-pasting from that documentation page here, you can read it and ask if you need any help with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM