0
我想解決在一個控制器中插入/更新。這是我的代碼:Symfony2和更新條目
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
。
findBy返回數組。如果你只想要一個對象:findOneBy – Cyprian