用于类型querybuilder symfony 4的循环(选择-选项选择)

问题描述 投票:0回答:1

你好,我想从数据库中获取最大值,因为我已经成功了,但是我在列表框示例4中只有最大值,如果最大值获取,我想在列表框[1,2,3,4]中获取最大值在数据库中是1我只有一个而示例是2我想[1,2]

DBlistbox并希望:

<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

由于

<select>
<option value="4">4</option>
</select>

Controller:

$form = $this->createForm(ReservationType::class, $booking, ['id_service' => $service]);

存储库:

public function findOneById($service)
    {
        return $this->createQueryBuilder('t')
            ->andWhere('t.service = :service')
            ->setParameter('service', $service)
            ;
   }

类型:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $id_service = $options['id_service'];
        $builder
            ->add('nbPlaces', EntityType::class, [
                'class' => Transport::class,
                'label' => 'Nombre de places',
                'attr' => [
                    'placeholder' => "Nombre de places",
                ],
                'query_builder' => function (TransportRepository $transportRepository) use ($id_service) {
                    $req = $transportRepository->findOneById($id_service);
                    return $req;

                },
                'choice_label' => 'nbPlaces',
                'choice_value' => 'nbPlaces'

            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Reservation::class,
            'id_service' => null
        ]);
    }

更详细的初始主题:GET max value form since DB symfony 4

非常感谢

php symfony symfony4
1个回答
0
投票

有三种方法可以做到这一点。

第一种方法:使用关系

我不完全了解项目,但从我的角度来看,我将重新设计架构并在ReservationTransport之间设置一个ManyToOne关系。并在配置运输字段中显示地点数。此方法的缺点是,由于存在这种关系,您可能无法删除Transport实体而不删除相关的Reservations或在删除Transport实体之前中断它们之间的关系。

第二种方法:DataTransformer

您可以使用DataTransformer进行此操作,显示Transport实体的列表(使用right属性显示),并保存从所选实体中提取的Integer。在这里看看:https://symfony.com/doc/current/form/data_transformers.html#harder-example-transforming-an-issue-number-into-an-issue-entity

第三种方法:将重新发送记录作为选项。

您可以通过选项将传输存储库传递给FormType,在构建表单之前获取数据,并使用数组作为nbPlace属性的ChoiceType的值。

© www.soinside.com 2019 - 2024. All rights reserved.