php Doctrine尝试插入空对象Symfony 5.4

nlejzf6q  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(134)

提交表单时,有两个值 可供选择:工作计划日和工作计划小时。
如果指定了workplanninghour,则在workplanninghour表中插入一行,反之亦然
当我提交一个workPlanningHour时,我想阻止doctrine插入一个带有空数据的workPlanningDay对象,我尝试使用$form-〉remove但是没有成功。
以下是我要求数据:

{"statuses":[6],"work_planning":1,"work_planning_hour":{"count_jrtt":1,"end_at":"31-12","jrtt":true,"lower_hour_jrtt":35,"nbr_jrtt":24,"start_at":"01-01","upper_hour_jrtt":39},"work_planning_period":0,"working_time_aspect":{"days":["MON","TUE","WED","THU","FRI"],"time":0,"weekly_hours":35}}

下面是我带有EventListener的窗体:

class WorkingTimeParametersRESTType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('work_planning', Field\ChoiceType::class, array(
                'property_path'     => 'workPlanning',
                'required'          => true,
                'choices'           => WorkingTimeParameters::getAllowedWorkPlannings(),
            ))
            ->add('work_planning_period', Field\ChoiceType::class, array(
                'property_path'     => 'workPlanningPeriod',
                'required'          => false,
                'choices'           => WorkingTimeParameters::getAllowedWorkPlanningPeriods(),
            ))
            ->add('working_time_aspect', WorkingTimeAspectType::class, array(
                'property_path' => 'workingTimeAspect',
                'required'      => true,
                'constraints'   => array(new Valid())
            ))
            ->add('work_planning_hour', WorkPlanningHourType::class, array(
                'property_path' => 'workPlanningHour',
                'required'      => true,
                'constraints'   => array(new Valid())
            ))
            ->add('work_planning_day', WorkPlanningDayType::class, array(
                'property_path' => 'workPlanningDay',
                'required'      => true,
                'constraints'   => array(new Valid())
            ))
            ->add('statuses', Field\CollectionType::class, array(
                'entry_type'    => CategoryIntegerType::class,
                'entry_options' => array(
                    'category_type' => Category::TYPE_STATUS,
                ),
                'allow_add'         => true,
                'allow_delete'      => true,
                'required'          => true,
                'error_bubbling'    => false,
            ))
            ->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'))
            ->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
                /** @var WorkingTimeParameters $contract */
                $parameters = $event->getData();
                $form = $event->getForm();
//                dd($form->has('work_planning_day'));
                // Remove useless work planning
                
                if ($parameters->getWorkPlanning() == Contract::WORK_PLANNING_HOUR) {
                    $parameters->setWorkPlanningDay(null);
                    $form->remove('work_planning_day');
                   
                }
                else if ($parameters->getWorkPlanning() == Contract::WORK_PLANNING_DAY) {
                    $parameters->setWorkPlanningHour(null);
                    $form->remove('work_planning_hour');
                }
                else {
                    dd('rerer');
                    $form->remove('work_planning_day');
                    $form->remove('work_planning_hour');
                    $parameters->setWorkPlanningDay(null);
                    $parameters->setWorkPlanningHour(null);
                    $parameters->setWorkPlanningPeriod(null);
                }
            })

        ;
    }

    /**
     * @param FormEvent $event
     */
    public function onPreSetData(FormEvent $event)
    {
        $rttParameters = $event->getData();
        $form = $event->getForm();

        // The company can be set only on creation
        if (!$rttParameters || !$rttParameters->getId()) {
            $form->add('company', CompanyIntegerType::class, array(
                'required'  => true,
            ));
        }
    }

以下是“理论”试图提出的要求:

INSERT INTO w2d_tm_work_planning_hour (startAt, endAt, jrtt, countJrtt, lowerHourJrtt, upperHourJrtt, nbrJrtt) VALUES ('1970-01-01 00:00:00', '1970-12-31 00:00:00', 1, 1, 35, 39, 24);

UPDATE w2d_tm_work_planning_day SET startAt = NULL, endAt = NULL, nbrDays = NULL, nbrJrtt = NULL WHERE id = 1;

如何使该学说不发出具有空值的第二个请求?

6ss1mwsb

6ss1mwsb1#

Symfony表单类型不能处理不同的实体,这取决于你在里面选择的选项。这不是Symfony表单类型应该被使用的方式。这就是为什么data_class属性是为一个实体设置的。
您最好的选择是创建两个不同的FormType,并根据用户在前端选择的内容只提交所需的一个。(简单地使用一点JavaScript)这样做的第二个好处是,您将拥有更多可理解和可维护的代码。

相关问题