1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Movies\Repository; |
6
|
|
|
|
7
|
|
|
use App\Guests\Entity\GuestSession; |
8
|
|
|
use App\Movies\Entity\Movie; |
9
|
|
|
use App\Movies\Entity\MovieRecommendation; |
10
|
|
|
use App\Users\Entity\User; |
11
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
12
|
|
|
use Doctrine\ORM\Query; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @method Movie|null find($id, $lockMode = null, $lockVersion = null) |
18
|
|
|
* @method Movie|null findOneBy(array $criteria, array $orderBy = null) |
19
|
|
|
* @method Movie[] findAll() |
20
|
|
|
* @method Movie[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
21
|
|
|
*/ |
22
|
|
|
class MovieRecommendationRepository extends ServiceEntityRepository |
23
|
|
|
{ |
24
|
|
|
public function __construct(RegistryInterface $registry) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($registry, MovieRecommendation::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function findAllByMovieAndUser(int $movieId, int $userId) |
30
|
|
|
{ |
31
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
32
|
|
|
|
33
|
|
|
$sql = 'SELECT DISTINCT ON(mr.recommended_movie_id) mr.recommended_movie_id, mr.rate, umr.user_id, movies.*, mt.*, uwm.* |
34
|
|
|
FROM ( |
35
|
|
|
SELECT mr.recommended_movie_id, COUNT(mr.recommended_movie_id) rate |
36
|
|
|
FROM movies_recommendations mr |
37
|
|
|
WHERE mr.original_movie_id = :movie_id |
38
|
|
|
GROUP BY mr.recommended_movie_id |
39
|
|
|
ORDER BY rate |
40
|
|
|
) mr |
41
|
|
|
LEFT JOIN movies_recommendations umr ON umr.recommended_movie_id = mr.recommended_movie_id AND umr.user_id = :user_id |
42
|
|
|
LEFT JOIN movies ON movies.id = mr.recommended_movie_id |
43
|
|
|
LEFT JOIN movies_translations mt ON movies.id = mt.movie_id |
44
|
|
|
LEFT JOIN users_watched_movies uwm ON uwm.movie_id = mr.recommended_movie_id AND uwm.user_id = :user_id'; |
45
|
|
|
|
46
|
|
|
$statement = $connection->prepare($sql); |
47
|
|
|
$statement->bindValue('movie_id', $movieId); |
48
|
|
|
$statement->bindValue('user_id', $userId); |
49
|
|
|
|
50
|
|
|
$statement->execute(); |
51
|
|
|
|
52
|
|
|
return $statement->fetchAll(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|