2013-05-02 51 views
2

这源于这个问题,但我的问题稍微修改表单标签:Odd many-to-many form rendering with symfony and doctrine显示AA相关实体属性值在symfony中

我的实体是一级方程式一对多与FormulaColor多到一个彩色。

式(Id,代码,姓名) FormulaColor(formula_id,COLOR_ID,百分比) 颜色(ID,代码,姓名)

公式可以具有一种或多种颜色,并且每个颜色构成的百分比那个公式。

我试图做一个公式编辑类型,它将显示给定公式的百分比字段以及颜色 - >标签名称的每个百分比字段的标签或标题。我已经显示了公式的百分比字段,但我想用颜色名称来标记每个字段。我怎样才能做到这一点?我必须以某种方式使用查询构建器吗?

我有一个FormulaAddEditType,看起来像这样:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('code', null, array(
       'label' => 'Code' 
      )) 
     ->add('name', null, array(
       'label' => 'Name' 
      )); 

    $builder->add('formulaColors', 'collection', array(
      'type' => new FormulaColorType(), 
      'allow_add' => true, 
      'allow_delete' => true, 
      'prototype' => true, 
     )); 
} 

然后,FormulaColorType:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('percentage', 'number', array(
      'label' => new ColorAddEditType() 
     )); 
} 

ColorAddEditType

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('code', null, array(
       'label' => 'Code' 
      )) 
     ->add('name', null, array(
       'label' => 'Name' 
      )) 
    ; 
} 

控制器动作

/** 
* @Route("/formulas/{id}/edit") 
* @Method({"GET", "POST"}) 
* @Template() 
*/ 
public function editAction(Request $request, $id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $formula = $em->getRepository('PrismPortalCommonBundle:Formula')->find($id); 

    if (!$formula) { 
     throw $this->createNotFoundException('Unable to find Formula entity.'); 
    } 

    $form = $this->createForm(new FormulaAddEditType(), $formula); 

    if ($request->isMethod('POST')) { 
     $form->bind($request); 

     if ($form->isValid()) { 

      $em->persist($formula); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('prism_portal_admin_dashboard_index')); 
     } 
    } 

    return array(
     'formula' => $formula, 
     'form' => $form->createView() 
    ); 
} 

我能得到的形式事件订阅我想要的结果。订户是这样的:

class AddPercentFieldSubscriber implements EventSubscriberInterface 
{ 
    public static function getSubscribedEvents() 
    { 
     // Tells the dispatcher that you want to listen on the form.pre_set_data 
     // event and that the preSetData method should be called. 
     return array(FormEvents::PRE_SET_DATA => 'preSetData'); 
    } 

    public function preSetData(FormEvent $event) 
    { 
     $data = $event->getData(); 
     $form = $event->getForm(); 

     // If it's not a new Formula, then I want to show the percentage fields. 
     if ($data) { 
      $form->add('percentage', 'text', array(
        'label' => $data->getColor()->getCode(), 
       )); 
     } 
    } 
} 
+0

请将控制器代码发布到创建此表单的位置。 – Lighthart 2013-05-02 14:42:06

+0

@Lighthart我已经添加了控制器的动作。 – 2013-05-02 17:18:48

+0

这些标签仅适用于现有公式? – Lighthart 2013-05-02 17:37:21

回答

2

我做了一些猜测到你的实体是什么样子,但我认为这大概是你想要的,是这样的:

FormulaAddEditType

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $entity=$builder->getData(); 
    $colors=$entity->getColors()); 

    $builder 
    ->add('code', null, array(
     'label' => 'Code' 
     )) 
    ->add('name', null, array(
     'label' => 'Name' 
     )); 

    $colors->map(function ($color) use ($builder) { 
     $builder->add($color->getName() 
      , null, 
      array(
       'label' => $color->getName() 
       ) 
      ) 
    }); 
} 
+0

感谢lighthart,我实际上只是能够得到一些抛在一起的东西,看起来和你所做的非常相似,除了我使用表单事件订阅者(请参阅我编辑过的帖子以查看我所做的)。你认为一种方法比另一种更好吗? – 2013-05-02 17:59:25

+0

我没有订阅者的经验。如果它能够工作,而且你擅长它,那么这是最好的。请张贴您的方法作为您自己的答案,并接受答案,以便其他人不会试图寻找其他解决方案。 – Lighthart 2013-05-02 19:23:01

+0

'$ colors = $ entity.getColors());'什么? – NDM 2015-05-20 12:13:59