Symfony在一个表单上编辑并保留多个实体

时间:2016-07-08 04:52:57

标签: symfony doctrine-orm twig

我有一个名为Objective的实体及其属性称为' weight'我可以一次编辑一个目标..现在我想在一个表单中编辑所有权重,并使用Doctrine将它们保存在DB中。

这个问题帮助我将所有客观权重放在一个表格上。 Edit multiple entities in one form

现在我正在四处寻找如何通过单击保存按钮从数据库中保存表单中的所有权重值。

以下是我的代码...

这是控制器动作: -

makeMapEvents = (insertProps) => {
  fetch("./json/meetup.json").then((response) => {
    return response.json()
  }).then((response) => {
    this.setState({events: response});
  }); // <-- Typo here. The closing parenthesis is missing
};

这是FormType: -

/**
 * @Route("/mou/objectives/manage", name="objective_manage")
 */
public function manageAction(Request $request){  

    $objs = $this->getDoctrine()
    ->getRepository('AppBundle:Objective')
    ->findAll();

    $form = $this->createFormBuilder()
    ->add('weight', 'collection', array(
        'type' => new WeightType() ,
        'allow_add' => false,
        'allow_delete' => false,
        'label' => false))
    ->add('save', 'submit')
    ->getForm();

    $form->setData(array('weight' => $objs));

    /* Till here things are fine */

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        /* **Here I need help how to persist all weight values.. maybe inside a loop.. hence This is the missing piece** */

        foreach($objs as $obj){
            $obj =new Objective();
            $obj = $em->getRepository('AppBundle:Objective')->find($obj);
            $obj->setWeight($obj); 
            $em->persist($obj);

        }
        $em->flush();    
        return $this->redirectToRoute('objective_manage');
    } 

    return $this->render('keyobjective/manage.html.twig', array(
        'objs' => $objs,
        'form' => $form->createView()
        ));
}

这是twig模板: -

class WeightType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('weight','text', array('label' => false));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Objective'
            //'data_class' => null
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_objective';
    }
}

任何建议的方法.. !!

1 个答案:

答案 0 :(得分:0)

不知怎的,我想出了...在表单句柄请求之后我创建了一个循环来遍历正在编辑的目标的数量,然后在其中另一个foreach循环,它将从Form接收的权重值转换为setWeight()方法一个接一个: -

/**
 * @Route("/mou/objectives/manage", name="objective_manage")
 */
public function manageAction(Request $request){  

    $objs = $this->getDoctrine()
    ->getRepository('AppBundle:Objective')
    ->findAll();

$count = count($objs);

            for($i =0; $i < $count; $i++){
                $objsarray =  new Objective();
            }

            $form = $this->createFormBuilder()
            ->add('weight', 'collection', array(
                'type' => new WeightType() ,
                'allow_add' => false,
                'allow_delete' => false,
                'options' => array('label' => false),
                ))
            ->add('save', 'submit')
            ->getForm();

            $form->setData(array('weight' => $objs));
            $form->handleRequest($request);
            if($form->isSubmitted() && $form->isValid()){
                $em = $this->getDoctrine()->getManager();

                for($i=0; $i < count($objs); $i++){
                    $obj = new Objective();
                    $obj = $em->getRepository('AppBundle:Objective')->find($objs[$i]);
                    $weight = $form->get('weight')->get($i)->getData();

                    foreach($weight as $val){
                        $obj->setWeight($val);
                        $em->persist($obj);
                    }           
                }
                $em->flush();
                $this->addFlash(
                    'notice',
                    'Objective weight updated'
                    );
                return $this->redirectToRoute('objective_manage');
            }
            return $this->render('keyobjective/manage.html.twig', array(
                'objs' => $objs,
                'form' => $form->createView()
                ));
        }
相关问题