我的问题是,我有桌子movie_status,我有两个外键:"电影"和"谁"。我正在尝试进行DQL查询,在那里我找到按行的对象,id的外键,但它什么都没找到。
实体:
/**
*
* @var type
*
* @ORM\ManyToOne(targetEntity="movies")
* @ORM\JoinColumn(name="movie", referencedColumnName="id")
*/
private $movie;
/**
*
* @var type
*
* @ORM\ManyToOne(targetEntity="list_base")
* @ORM\JoinColumn(name="who", referencedColumnName="id")
*/
private $who;
控制器
/**
*
* @Route("/introduction/updateTime", options={"expose"=true}, name="int_update_time")
* @Method("post")
*/
public function updateTimeAction(Request $request) {
$time = floor($request->request->get('time'));
$movieUsId = $request->request->get('film_id');
$userId = $request->request->get('id');
$em = $this->getDoctrine()->getManager();
$movie = $em->getRepository('MainBundle:movies')->findOneBy(['useable_id' => $movieUsId]);
$user = $em->getRepository('MainBundle:list_base')->findOneById($userId);
//here it finds nothing
$moviesStatus = $em->getRepository('MainBundle:movies_status')->findOneBy(['who' => $user, 'movie' => $movie]);
$currentTime = $moviesStatus->getFilmTime();
$moviesStatus->setFilmTime($time);
$em->persist($moviesStatus);
$em->flush();
if ($time == $currentTime) {
return new Response("koniec");
}
return new Response("ok " . $time . " " . $userId . " " . $movieUsId . " " . $movie->getId() . " " . $user->getFirstName());
}
错误始终是:
Call to a member function getFilmTime() on null
在控制器中我也尝试按ID搜索,但仍然没有。 我还尝试在存储库中创建自定义查询:
public function findOneMS($user, $movie) {
$q = $this->getEntityManager()
->createQuery('SELECT u FROM MainBundle:movies_status u WHERE u.who = :userId AND u.movie = :movieId')
->setParameter('userId', $user)
->setParameter('movieId', $movie)
->getSingleResult();
return $q;
}
是的,数据库中存在行。 提前谢谢。
答案 0 :(得分:0)
您的两个多对多关系创建了两个额外的表,您应该在请求中使用这些表。
查询应如下所示:
{{1}}
然后按返回的ID加载您的实体。