Home > Blockchain >  Collection type new added elements relation null
Collection type new added elements relation null

Time:10-23

I have an entity Classroom with many Students entities, a student belongs to only 1 classroom

my classroom form:

$builder
    ->add('name')
    ->add('students', CollectionType::class, [
        'entry_type' => StudentType::class,
        'allow_add' => true,
        'allow_delete' => true,
    ])
;

when new students are added, the classroom students collection has the newly added students, while the new students field classroom is NULL, i added event cascade persistence on my classroom entity

@ORM\OneToMany(targetEntity=Student::class, mappedBy="classroom", orphanRemoval=true, cascade={"persist"})

but i'm still getting error that the classroom field in the new students is NULL

Column 'classroom_id' cannot be null

does anyone know why ?

CodePudding user response:

actualy symfont doesn't call your method addStudent, because the option by_reference has true by default, so to force symfony to use your method, you must set by_reference to false.

->add('students', CollectionType::class, [
        'entry_type' => StudentType::class,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
    ])
  • Related