我想在一个控制器中解决插入/更新问题。这是我的代码:
public function setOwnershipAction(Request $request) {
$session = $this->get('security.context')->getToken()->getUser();
$em = $this->getDoctrine()->getManager();
$game = $em->getRepository('GameShelfGamesBundle:Game')->find($request->request->get('hash'));
$user = $em->getRepository('GameShelfUsersBundle:User')->find($session->getId());
$typo = $em->getRepository('GameShelfUsersBundle:OwnState')->find($request->request->get('setownstate')['id']);
$plat = $em->getRepository('GameShelfGamesBundle:Platforms')->find($request->request->get('platformown')['id']);
$check = $em->getRepository('GameShelfUsersBundle:Own')->findBy(array(
'user' => $user,
'game' => $game
));
if(!$check) {
$own = new Own;
$own->setGame($game);
$own->setUser($user);
$own->setTypo($typo);
$own->setPlatforms($plat);
$own->setUpdated(date("Y-m-d H:i:s"));
$em->persist($own);
$em->flush();
} else {
$check->setTypo($typo);
$em->flush();
}
}
现在,插入(在if(!$check)
变为真之后)有效,但在else
它不点击之后。我的错误是Fatal error: Call to a member function setTypo() on a non-object
。