How should a `FormType` be structured in order to include an `EntityType` field which doesn't exist in the `FormType` entity?

后端 未结 1 1146
-上瘾入骨i
-上瘾入骨i 2021-01-24 21:58

Taking the below as an example, OrderType is based on the entity Order. The form that is required needs to contain the following two EntityType dropdowns within it: <

相关标签:
1条回答
  • 2021-01-24 22:49

    As you say, adding a Category property to the Order entity just for forms is less than ideal. What I would do is make a OrderCategoryType and pass in Order and Category as an array.

    // Controller
    $order = new Order();
    $category = new Category();  // Or create from $order->getProduct()
    $data = ['order' => $order, 'category' => $category);
    
    $form = $this->createFormBuilder($data)
      ->add('order',new OrderType(),
      ->add('category',new CategoryType()
    );
    

    You will have to do some messing around to keep everything in sync but it should work just fine.

    0 讨论(0)
提交回复
热议问题