【问题标题】:Set default value for entity type in Symfony2在 Symfony2 中设置实体类型的默认值
【发布时间】:2013-11-03 20:55:16
【问题描述】:

我不知道如何在 symfony2 中为实体类型设置默认值。我的代码如下所示:

$rewardChoice = $this->createFormBuilder($reward)
    ->add('reward_name', 'entity', array(
        'class' => 'FuelFormBundle:Reward',
        'property' => 'reward_name',
        'data' => 2,
        'query_builder' => function(EntityRepository $er){
            return $er->createQueryBuilder('r')
                ->where('r.active = 1')
                ->groupBy('r.reward_id')
                ->orderBy('r.reward_name', 'DESC');

        },
    ))
    ->getForm();

但是,您需要交出正在使用的对象才能使其正常工作。我的答案如下。

我在这方面找到了很多不同的答案,但他们都重新构建了表单的构建方式。这要容易得多。

【问题讨论】:

    标签: php symfony types


    【解决方案1】:

    所以我找到了很多答案来完成这项工作,但他们似乎都以另一种方式重组了要构建的表单,但是我发现设置对象效果最好,所以我想我会发布我的解决方案,以防有人跑再次进入这个问题。

    这是我在控制器中的代码。

    // this is setting up a controller and really isn't important 
    $dbController = $this->get('database_controller');
    
    // this is getting the user id based on the hash passed by the url from the
    // database controller
    $user_id = $dbController->getUserIdByHash($hash);
    
    // this is getting the Reward Entity.  A lot of times you will see it written as 
    // $reward = new Reward however I am setting info into reward right away in this case
    $reward = $dbController->getRewardByUserId($user_id);
    
    
    $rewardChoice = $this->createFormBuilder($reward)
        ->add('reward_name', 'entity', array(
            'class' => 'FuelFormBundle:Reward',
            'property' => 'reward_name',
    
            // I pass $reward to data to set the default data.  Whatever you
            // assign to $reward will set the default value.  
            'data' => $reward,  
            'query_builder' => function(EntityRepository $er){
                    return $er->createQueryBuilder('r')
                        ->where('r.active = 1')
                        ->groupBy('r.reward_id')
                        ->orderBy('r.reward_name', 'DESC');
            },
        ))
        ->getForm();
    

    我希望这能让事情更清楚。我看到了很多相同的问题,但没有这个解决方案。

    【讨论】:

      【解决方案2】:

      最好的方法是在创建表单之前设置奖励名称。例如:

      $reward->setRewardName('your_relationship_reference_here');
      
      $rewardChoice = $this->createFormBuilder($reward)
      

      使用data 字段可能会导致问题。

      【讨论】:

      • 是的,看来您的解决方案是最优雅的解决方案。关于data: "...这意味着当表单编辑一个已经持久化的对象时,对象值也会被覆盖,导致它在提交表单时丢失其持久值。" @987654321 @] 另一方面,empty_data 不会影响表单的显示 - 它只是在提交时提供 default 值。 [2]
      • 是的,只有在创建新实体时才应该这样做,编辑时不要设置默认值。
      【解决方案3】:

      从表单的目标对象初始化值

      $article=2/*Id of item that you wont to set as default*/    
      $stock->setArtigo($this
                      ->getDoctrine()
                      ->getManager()
                      ->getRepository('AppBundle:Artigo')
                      ->find($article));
      
                  $form = $this->createForm('AppBundle\Form\StockType', $stock);
      

      【讨论】:

        【解决方案4】:

        对于 symfony 4,我们可以使用 'placeholder' => 'Choose an option',

        ->add('reward_name', EntityType::class, array(
                            'class' => Reward::class,
                            'choice_label' => 'name',
                            'placeholder' => 'Choose an option',
                        ))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-24
          • 1970-01-01
          • 1970-01-01
          • 2012-05-19
          • 2012-05-12
          相关资源
          最近更新 更多