我有一个多个EntityType,它从数据库中获取他的值,如下所示:
MyFormType.php
private $membersRepository;
public function __construct(MembersRepository $membersRepository)
{
$this->membersRepository = $membersRepository;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('trainer', EntityType::class, [
'multiple' => true,
'expanded' => true,
'class' => Members::class,
'choices' => $this->membersRepository->findAllByExecutive(),
'placeholder' => 'Ausbilder',
'label' => 'Ausbilder',
'attr'=> array('class'=>'form-control'),
])
我的带有findAllByExecutive()的存储库
/**
* @return Members[]
*/
public function findAllByExecutive()
{
return $this->createQueryBuilder('u')
->select('partial u.{id, firstname, lastname}')
->orderBy('u.lastname', 'ASC')
->add('where', 'u.executive = 1')
->getQuery()
->execute();
}
最后是我的控制器,将这些值放入数据库
if ($form->isSubmitted() && $form->isValid()) {
/** @var Dienstbuch $dienstbuch */
$dienstbuch = $form->getData();
//dd($dienstbuch->trainer);
$em->persist($dienstbuch);
$em->flush();
$this->addFlash('success', 'Eintrag gespeichert');
return $this->redirectToRoute('dienstbuch.neu');
}
答案 0 :(得分:0)
尝试
/**
* @return Members[]
*/
public function findAllByExecutive()
{
return $this->createQueryBuilder('u')
->select('partial u.{id, firstname, lastname}')
->orderBy('u.lastname', 'ASC')
->add('where', 'u.executive = 1')
->getQuery()
->setHint(Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD, 1)
->execute();
}