簡體   English   中英

從EntityType Symfony獲取價值

[英]Get value from EntityType Symfony

我無法從EntityType獲得價值。 我的最新版本為3.3.6。

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
    ));
 }
}

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'),
    ));
}

我嘗試了所有,但我不能接受TeamType的“名稱”值。 提交表格后,我會

foreach ($form["team"]->getData() as $value) {
   'name' => $value['name']

但該值為空。 如果我嘗試轉儲請求,並且值在那里。 我可以獲取其他值並將其保存在數據庫中。 只有EntityType我不能。 有人知道怎么辦嗎?

我想您正在使用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 Now中:

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(),
    ));

}

EntityType作為對象返回。 您應該使用模型獲取器功能。

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

這是一個類似的例子。

symfony如何從選擇中獲取選擇的數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM