public function viewMovie($movieId) { $movieView = $this->_movieViewRepository->findOneBy(array('id' => $movieId)); $movie = $this->_movieRepository->findOneBy(array('id' => $movieId)); if (!$movieView) { $movieView = new MovieView(); $movieView->setId($movie->getId()); $movieView->setViewCount($movie->getViewCount()); $this->_em->persist($movieView); $this->_em->flush($movieView); $this->_movieRepository->clear(); } $movieView->setViewCount($movieView->getViewCount() + 1); $rnd = rand(0, 100); if ($rnd < $this->_updateViewCountRatio) { $movie->setViewCount($movieView->getViewCount()); } $this->_updateMovieUrl($movieId, $rnd); }