Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/forms/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Symfony2:将用户条目持久化到数据库_Database_Forms_Symfony_Submit - Fatal编程技术网

Database Symfony2:将用户条目持久化到数据库

Database Symfony2:将用户条目持久化到数据库,database,forms,symfony,submit,Database,Forms,Symfony,Submit,我有一个Symfony2格式的表单,它显示在一个小树枝模板上。我想知道将用户条目保存到数据库中的控制器的业务逻辑是什么?我尝试了下面的代码,但它没有插入到数据库中。非常感谢: /** *@Route/lot,name=sort *@模板 */ 公共函数bestActionRequest$request { $quest=新任务; $form=$this->createformnewquesttype,$quest,array //“操作”=>$this->generateUrl'best',

我有一个Symfony2格式的表单,它显示在一个小树枝模板上。我想知道将用户条目保存到数据库中的控制器的业务逻辑是什么?我尝试了下面的代码,但它没有插入到数据库中。非常感谢:

/** *@Route/lot,name=sort *@模板 */ 公共函数bestActionRequest$request { $quest=新任务; $form=$this->createformnewquesttype,$quest,array //“操作”=>$this->generateUrl'best', '方法'=>'发布', ; $form->HandlerRequest$请求; 如果$entity->isValid{ $em=$this->getDoctrine->getEntityManager; $entity=$em->getRepository'IWABundle:quest'; $em->persist$entity; $em->flush; } 返回$this->render'IWABundle:Default:index.html.twig',数组 'form'=>$form->createView ;
} 您将实体$quest与实体管理器混淆:

/**
 * @Route("/lot", name="sort")
 * @Template()
 */
public function bestAction(Request $request)
{
    // 1) Create your empty entity
    $quest = new Quest();

    // 2) Create your form from its type and empty entity
    $form = $this->createForm(new QuestType(), $quest, array(
        'method' => 'POST',
    ));

    // 3) Handle the request
    $form->handleRequest($request);

    // 4) Check if the form is valid
    if ($form->isValid()) {
        // 5) Get the entity manager
        $em = $this->getDoctrine()->getEntityManager();

        // 6) Persist the new entity and flush the changes to the database
        $em->persist($quest);
        $em->flush();
    }

    return $this->render('IWABundle:Default:index.html.twig', array(
        'form' => $form->createView()
    ));
}

查看和上的官方文档。

使用表单时,您正在填写$quest对象的详细信息,但之后您将获得一个存储库,然后尝试将其保存到数据库中。如果删除$entity=$em->getRepository'IWABundle:quest';而是将$quest对象持久化到数据库中。