doutou1922 2016-03-02 07:24
浏览 13

Symfony,在一种形式中嵌入5或6个没有关系的实体

In my case i have 30 entities that there are no relation between them and every entities have two columns. I wanted to have 6 or 7 entities in the one form, but i don't know what is the best way to do it? this is my code ...

this is my Controller:

public function general1Action(Request $request)
{
    $example1 = new Example1();
    $example2 = new Example2();
    $example3 = new Example3();
    $example4 = new Example4();


    $formexample1 = $this->createForm('...Bundle\Form\Example1Type', $example1);
    $formexample2 = $this->createForm('...Bundle\Form\ Example2Type', $example2);
    $formexample3 = $this->createForm('...Bundle\Form\ Example3Type', $example3);
    $fprmexample4 = $this->createForm('...Bundle\Form\Example4Type', $example4);


    $example1->handleRequest($request);
    $example2->handleRequest($request);
    $example3->handleRequest($request);
    $example4->handleRequest($request);


    $em = $this->getDoctrine()->getManager();


    if ($example1->isSubmitted() && $example1->isValid()) {

            /**
            * To generate the value example1 first column(e1fc) and second       column(e1sc)
            */

            $prefix=$this->container->getParameter('prefix');         
            $e1fcCle=$em->getRepository("...Bundle:Example1")->genereCle('Example1',$prefix);
            $example1->sete1fc($e1fcCle);


            /**
            * To generate the value example1 second column(e1sc)
            */
            $example1->sete1sc("e1sc".($e1fcCle));

            /**
            * to check, the fields are not empty
            */ 
            if(($formexample2["e2sc"]->getData())!=""){
                $example2->sete1fc($e1fcCle);
                $em->persist($example2);
            } 
            if(($formexample3["e3sc"]->getData())!=""){
                $example3->sete1fc($e1fcCle);
                $em->persist($example3);
            }
            if(($formexample4["e4sc"]->getData())!=""){
                 $example4-> sete1fc($e1fcCle);                 
                $em->persist($example4);
            }

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


                return $this->forward('...Bundle:General...: general2',
                        array('E1FC' => $e1fcCle));
    }


    return $this->render('.../general1.html.twig', array(
        'example1' => $example1,
        'formExample1' => $formexample1->createView(),
        'formExample2' => $formexample2->createView(),
        'formExample3' => $formexample3->createView(),
        'formExample4' => $formexample4->createView(),
    ));

}` 

and this is my general1.html.twig :

{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(formExample1) }}
{{ form_row(formExample2.e2sc) }}
{{ form_row(formExample3.e3sc) }}
{{ form_row(formExample4.e4sc) }}
<input type="submit" value="Next" />
{{ form_widget(formExample1._token) }}  
{{ form_end(formExample1, {"render_rest":false}) }}
<ul>
    <li>
        <a href="{{ path('..._index') }}">Back to the list</a>
    </li>
</ul>
{% endblock %}`,

I have another question : when i try to use isValid() for the other form like this

if(($formexample2["e2sc"]->getData())!=""&& $example2->isValid())

i have this error: Fatal error: Call to a member function get...() on null

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。