Symfony -优化表单重复EntityType



目前我正在开发的网站是运行Symfony 3.4。

我的一个实体是Contact:我得到一个非常大的MySQL表,有~ 14000行联系人配置文件:

姓名电子邮件电话号码
分配
Brand Peter peter.brand@aol.com 49594885403 Bla blabla

这样做的一种方法是创建一个属性$clientChoices,在其中一次性存储您的repo->getAll()结果。为此,您必须将表单定义为服务:https://symfony.com/doc/3.4/form/form_dependencies.html.

然后注入EntityManager以在构造函数中获得所需的内容,如下所示:

<?php
namespace AppBundleForm;
/*****
** Here all my others 'use'
*****/
use AppBundleEntityContact;
use AppBundleRepositoryContactRepository;
class EventForm
{
private $clientChoices = [];
public function __constructor(EntityManagerInterface $entityManager)
{
$this->clientChoices = $entityManager->getRepository(Client::class)->getAll();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder

// A lot of ->add(), not revelant for my issue

->add('client1', EntityType::class, [
'class' => Client::class,
'choices' => $this->clientChoices,
])

->add('client2', EntityType::class, [
'class' => Client::class,
'choices' => $this->clientChoices,
}
])

// ->add('client3'), same as above
// ->add('client4'), same as above
// ->add('client5'), same as above
// ->add('client6'), same as above
;
}

相关内容

  • 没有找到相关文章

最新更新