fix(task): task creation broke on save - task_kind could reach the database as null

A new Task's taskKind property defaulted to null (the 0.18.x task rework), while
the task_kind column is NOT NULL: Doctrine writes every column explicitly, so
the column's own default never applied and any save whose form field did not
map failed with "1048 Column 'task_kind' cannot be null".

- Task::$taskKind now defaults to TaskKind::Task, like $status: the property
  is never null anymore and the getter is straight
- setTaskKind()/setStatus() accept null as "keep the current kind/status", so
  a missing form field can neither throw at mapping time nor wipe the value
- TaskType defaults an absent or empty taskKind to a regular task on PRE_SUBMIT
- regression tests: Tests/Unit/Entity/TaskTest.php
This commit is contained in:
alexandre_gain_mt3s committed 2026-11-21 14:31:49 +01:00
1 parent d6b3488cad
commit 603e564037
3 files changed
+110 -14

No files matched your search

+18
View File
@@ -22,6 +22,8 @@ use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
@@ -59,6 +61,22 @@ class TaskType extends AbstractType
'choice_label' => fn (TaskKind $k): string => $k->getLabelKey(),
]);
// A submitted form always carries a kind: an absent or empty taskKind (a browser
// POST that lost the select, or an ajax save) defaults to a regular task, so the
// NOT NULL column can never receive null (see Task::$taskKind).
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
$data = $event->getData();
if (!is_array($data)) {
return;
}
if (!isset($data['taskKind']) || '' === $data['taskKind']) {
$data['taskKind'] = TaskKind::Task->value;
$event->setData($data);
}
});
$builder->add('type', ChoiceType::class, [
'label' => 'mt3s.marketing_program.task.field.type',
'label_attr' => ['class' => 'control-label'],