我想在实体字段中显示数据,但是当我打开编辑表单时,数据库中的数据不显示在实体字段中,这是我的代码:
<?php
namespace Projects\ProjectsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Response;
class MilestonesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('description')
->add('dueDate','datetime',array('label' => false,'data'=>new \DateTime(),'attr'=>array('style'=>'display:none;')))
->add('companyName', 'entity', array('class' => 'ClientsClientsBundle:Company','query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
'label' => 'Company id',
'property' => 'companyName',
))
->add('firstname', 'entity', array('class' => 'ClientsClientsBundle:Clients','query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
'label' => 'Client id',
'property' => 'firstname',
))
->add('isPrivate','choice', array('choices' => array('yes' => 'yes', 'no' => 'no')))
->add('startedAt','datetime',array('label' => false,'data'=>new \DateTime(),'attr'=>array('style'=>'display:none;')))
->add('completedOn','datetime',array('label' => false,'data'=>new \DateTime(),'attr'=>array('style'=>'display:none;')))
->add('createdDatetime','datetime',array('label' => false,'data'=>new \DateTime(),'attr'=>array('style'=>'display:none;')))
->add('updatedDatetime','datetime',array('label' => false,'data'=>new \DateTime(),'attr'=>array('style'=>'display:none;')))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Projects\ProjectsBundle\Entity\Milestones'
));
}
public function getName()
{
return 'projects_projectsbundle_milestonestype';
}
}
这是我的编辑表格:
<form action="{{ path('milestones_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
<input type="hidden" name="_method" value="PUT" />
{{ form_widget(edit_form) }}
<p>
<button type="submit" class="btn btn-success">Next <i class="icon-chevron-right"></i></button>
</p>
</form>
答案 0 :(得分:2)
首先你需要获取实体
$milestone = $this->em->getRepository('Projects\ProjectsBundle\Entity\Milestones')->find($id);
接下来,您需要传递实体以使用MilestonesType作为类型base
创建表单$form = $this->createForm(new MilestonesType(), $milestone);
然后创建表单视图
$form->createView();