gpt4 book ai didi

php - Doctrine2 - FindOneBy 外键

转载 作者:行者123 更新时间:2023-11-30 22:02:54 25 4
gpt4 key购买 nike

我的问题是,我有表 movies_status,其中有两个外键:“movie”和“who”。我正在尝试进行 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;

Controller

/**
*
* @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

在 Controller 中,我也尝试通过 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;
}

是的,行存在于数据库中。提前致谢。

最佳答案

您的两个多对多关系已经创建了两个额外的表,您应该在您的请求中使用它们。

查询应该如下所示:

SELECT s.id FROM movies_status s, movies_status_movies m, movies_status_list_base u WHERE s.id = m.movies_status_id AND s.id = u.movies_status_id AND m.movie_id = :movieId AND u.list_base_id = :userId

然后通过返回的 id 加载您的实体。

关于php - Doctrine2 - FindOneBy 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42807222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com