我正在努力寻找解决这个问题的正确方法。
我有一个带有下拉列表的表单,我希望通过使用EntityType字段调用DB来动态生成。一切都很完美但是我无法在编辑时将其默认为之前的值。表单中的所有其他字段(所有文本)都默认为上一个值,如果我使用ChoiceType手动构建字段,则默认为预期。
我尝试过使用'数据'手动设置它的选项,但它不起作用。如果没有将它作为ChoiceType构建并通过控制器发送数据还有其他我应该尝试的事情吗?
这是我的表单类TerminologyType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
class TerminologyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Event listener added to prevent users from editing the unique
// code after it has been created.
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$terminology = $event->getData();
$form = $event->getForm();
// check if the Terminology object is "new"
// This should be considered a new "Terminology" and user must enter a code
if (!$terminology || null === $terminology->getCode()) {
$form->add('code');
}
});
$builder
->add('name')
->add('version')
->add('description')
->add('status', EntityType::class, [
'class' => 'AppBundle:CodeSet',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.terminologyCode = :code')
->setParameter('code', 'entity_status')
->orderBy('c.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'code',
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Terminology',
));
}
}
控制器
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Terminology;
use AppBundle\Form\TerminologyType;
/**
* Terminology controller.
*
* @Route("/admin/terminology")
*/
class TerminologyController extends Controller
{
/**
* Lists all Terminology entities.
*
* @Route("/", name="terminology_index")
* @Method("GET")
*/
....
/**
* Displays a form to edit an existing Terminology entity.
*
* @Route("/{code}/edit", name="terminology_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Terminology $terminology)
{
$deleteForm = $this->createDeleteForm($terminology);
$editForm = $this->createForm('AppBundle\Form\TerminologyType', $terminology);
$editForm->handleRequest($request);
$em = $this->getDoctrine()->getManager();
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em->persist($terminology);
$em->flush();
return $this->redirectToRoute('terminology_show', array('code' => $terminology->getCode()));
}
return $this->render('terminology/edit.html.twig', array(
'terminology' => $terminology,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
实体
<?php
// src/AppBundle/Entity/Terminology.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @Gedmo\Loggable
* @ORM\Table(name="ts_terminology")
* @Gedmo\SoftDeleteable(fieldName="dateDeleted", timeAware=false)
* @UniqueEntity("code")
*
*/
class Terminology extends TerminologyServices
{
/**
* @ORM\Column(type="string", length=75)
* @ORM\OneToMany(targetEntity="CodeSet", mappedBy="terminologyCode")
* @ORM\Id
*/
protected $code;
/**
* @ORM\Column(type="string", length=75, nullable=true)
* @ORM\OneToOne(targetEntity="CodeSet")
* @ORM\JoinColumn(name="status", referencedColumnName="code")
* @Gedmo\Versioned
*/
protected $status;
答案 0 :(得分:0)
我能够找到问题所在。在状态的实体定义中,我在注释中定义了列类型,长度等。 @ORM \ Column(type =“string”,length = 75,nullable = true)
通过删除它,它开始默认为最后输入的值(但是这随后打破了其他事情)。我相信这与EntityType有关,期望一个对象,但实体类型被定义为一个字符串,只提供它(这解释了为什么ChoiceType选项有效)。