从EntityType Symfony获取价值

时间:2017-09-28 14:26:10

标签: php html symfony frameworks

我无法从EntityType获取价值。我的最新版本是3.3.6。

$('#input-date')
    .datepicker()
    .on('changeDate', function(e){
        setTimeout(function () {
            $('#date-form').submit();
        });
    });
class BuildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array 
       $options)
    {
    $builder
    ->add('title', TextType::class)
    ->add('save', SubmitType::class, array('label' => 'Create Post'))
    ->add('team', CollectionType::class, array(
        // each entry in the array will be an "email" field
        'entry_type' => TeamType::class,
        // these options are passed to each "email" type
        'entry_options'  => array(
            'attr'      => array('class' => 'form-control'),
        ),
        'label' => false,
        'allow_add' => true,
        'prototype' => true,
        'mapped' => false
    ));
 }
}

我尝试了所有但我无法理解' name' TeamType。提交表格后,我做

class TeamType extends AbstractType
{
     public function buildForm(FormBuilderInterface $builder, array 
      $options)
    {

    $builder
    ->add('name', EntityType::class, array(
        'placeholder' => 'Choice a champ',
        'required' => true,
        'class' => 'AppBundle:Champions',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('c')
            ->orderBy('c.name', 'ASC');
        },
        'choice_label' => 'name',
        'choice_value' => 'id',
        'attr' => array('class' => 'dropdown'),
    ));
}

但该值为空。如果我尝试转储请求,价值就在那里。其他值我可以得到它并保存在数据库中。只有EntityType我不能。 有人知道怎么办?

2 个答案:

答案 0 :(得分:1)

我认为你正在使用ManyToOne关系。

使用AJAX

如果您在提交后尝试获取数据,可以先在视图中执行此操作:

   $('#elementWhereYouAreTEAMType').find('input').each(function (i,v) {
       var valTeam = $(this).val(); //take value

       // adding data, create an associative array.
       formData.append("team["+ i +"]", valTeam);

   });

您必须将formDatadata参数放在ajax JQuery中 现在,服务器端:

public function createTeamNow(Request $request) {// ajax

  $teams = $request->request->get('team'); // getting array

  if(!is_null($teams)) {// if user added team
    foreach ($teams as $team) {
       // dump($team);

       //create an instance for each element, it does not replace a data with the above
       $teamType = new TeamType(); 
       $teamName->setName($team);
       $this->em->persist($teamType);
    }
  }

}

没有AJAX

/**
 * @Route("/slim/1" , name="data_x")
 */ 
public function slimExampleAction(Request $request)
{

    $form = $this->createForm(TeamType::class);

    $form->handleRequest($request);

    if ($form->isSubmitted() /*&& $form->isValid()*/) {
        // When you've ManyToOne relationship, that field returns ArrayCollection class, it has several method to get data on differents ways, iterate with it, for example toArray is one
        $data = $form->getData();
        dump($data->getDescription()->toArray());die;
    }

    return $this->render('AppBundle:view.html.twig', array(
        'form' => $form->createView(),
    ));

}

答案 1 :(得分:1)

EntityType作为对象返回。你应该使用模型getter函数。

$form->get('name')->getData()->getId(); // getName() vs..

这是一个类似的例子。

symfony how get selected data from select