在symfony中将相关图元属性值显示为表单标签

在symfony中将相关图元属性值显示为表单标签,symfony,Symfony,这源于这个问题,但我的问题略有改变: 我的实体是公式一对多和公式颜色多对一和颜色 公式(id、代码、名称) 公式颜色(公式id、颜色id、百分比) 颜色(id、代码、名称) 一个公式可以有一种或多种颜色,每种颜色占该公式的百分比 我正在尝试创建一个公式编辑类型,该类型将显示给定公式的百分比字段以及标签的颜色->名称的每个百分比字段的标签或标题。我已经显示了公式的百分比字段,但我想用颜色名称标记每个字段。我该怎么做?我是否需要使用querybuilder 我有一个FormulaAddEdit类型,

这源于这个问题,但我的问题略有改变:

我的实体是公式一对多和公式颜色多对一和颜色

公式(id、代码、名称) 公式颜色(公式id、颜色id、百分比) 颜色(id、代码、名称)

一个公式可以有一种或多种颜色,每种颜色占该公式的百分比

我正在尝试创建一个公式编辑类型,该类型将显示给定公式的百分比字段以及标签的颜色->名称的每个百分比字段的标签或标题。我已经显示了公式的百分比字段,但我想用颜色名称标记每个字段。我该怎么做?我是否需要使用querybuilder

我有一个FormulaAddEdit类型,如下所示:

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,
        ));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('percentage', 'number', array(
            'label' => new ColorAddEditType()
        ));
}
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(),
                ));
        }
    }
}
然后是公式ColorType:

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,
        ));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('percentage', 'number', array(
            'label' => new ColorAddEditType()
        ));
}
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(),
                ));
        }
    }
}
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()
    );
}
我能够在表单事件订阅服务器中获得我想要的结果。订阅服务器如下所示:

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

我对你的实体做了一些猜测,但我认为这大概就是你想要的,比如:

公式型

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

请在创建此表单的位置发布控制器代码。@Lighthart我已从控制器添加了操作。这些标签仅适用于现有公式?感谢Lighthart,我实际上刚刚能够得到一些东西,这些东西看起来与您所做的非常相似,只是我使用了一个表单事件订阅服务器(请参阅我编辑的帖子,以了解我所做的一切)。你认为一种方法比另一种好吗?我对订阅者没有经验。如果它有效,而且你对它很在行,那么这就是最好的。请将你的方法作为你自己的答案发布,并接受这个答案,这样其他人就不会试图找到其他解决方案了。
$colors=$entity.getColors())什么?啊。在那里我悄悄地讲了一分钟另一种语言。固定的。