将数据保存到数据库。坚持不工作

时间:2017-02-14 18:26:28

标签: database symfony

我写了一个控制器动作,它应该在这里向数据库添加一个元素(会议):

public function newAction(Request $request){
    $meeting = new Meeting();
    $meetingUser = new MeetingUser();
    $project = new Project();
    $projectName = "SocialPro";//$request->get('projectName');
    echo($projectName);
    $users = $this->getDoctrine()->getRepository('SocialProMeetingBundle:meetingUser')->findProjectUser($projectName);
    //$form = $this->createForm('SocialPro\MeetingBundle\Form\MeetingType', $meeting);
    //$form->handleRequest($request);
    //if ($form->isSubmitted() && $form->isValid()) {
    $userconn = $this->container->get('security.token_storage')->getToken()->getUser();
    echo($userconn->getId());
    if ($request->isMethod('POST')) {
        echo("message form");
        $role = $this->getDoctrine()->getRepository('SocialProMeetingBundle:meetingUser')->findUserRole($userconn)[0]['role'];
        $date = $request->get('date');
        if ($role == "PROJECT_MASTER" || $role == "TEAM_MASTER") {
            for ($i = 0; $i < count($users); $i++) {
                $meetings = $this->getDoctrine()->getRepository('SocialProMeetingBundle:meetingUser')->findMeetingUser($users[$i]['id'], $date);
            }
            if ($meetings == null || count($meetings) == 0) {
                $project = $this->getDoctrine()->getRepository('SocialProProjectBundle:Project')->findBy(array("name" = >$projectName));
                $meeting->setDescription($request->get('description'));
                $meeting->setDate(new \DateTime($request->get('date')));
                $meeting->setTime($request->get('time'));
                $meeting->setProjectName($request->get('projectName'));
                $meeting->setProject($project[0]);


                $meetingUser->setMeetings($meeting);
                $meetingUser->setUsers($userconn);
                var_dump($meetingUser);
                $meeting->setMeetingUser(array($meetingUser));
                //$project->setMeetings($meeting->getId());
                $em = $this->getDoctrine()->getManager();

                $em->persist($meeting);
                $em->persist($meetingUser);
                $em->flush();
                // $meetingUser->setUsers($request->get(''));
                return $this->redirectToRoute('reunion_show', array('id' = > $meeting->getId()));
            }
            else {
                echo("Membre indisponible");
            }
        }
        else {
            echo("Must be MASTER to create meeting");
        }
    }
    return $this->render('SocialProMeetingBundle::ajoutMeeting.html.twig', array('users' = >$users));
    // $em = $this->getDoctrine()->getManager();
    //$em->persist($meeting);
    //$em->flush($meeting);

    // return $this->redirectToRoute('meeting_show', array('id' => $meeting->getId()));
    //}

    //return $this->render('SocialProMeetingBundle:ajouMeeting', array(
    //  'meeting' => $meeting,
    //'form' => $form->createView(),
    //));
}

当我提交表单时,它会为我提供一个不可用的网站页面。我逐行测试,一切都很完美。原来问题在于

$em->persist($meeting);

我不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

你必须在调用persist后立即调用flush,如下所示:

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

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

然后它会持续存在。