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 App\Users\Entity\UserWatchedMovie; |
12
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
13
|
|
|
use Doctrine\ORM\Query; |
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method Movie|null find($id, $lockMode = null, $lockVersion = null) |
19
|
|
|
* @method Movie|null findOneBy(array $criteria, array $orderBy = null) |
20
|
|
|
* @method Movie[] findAll() |
21
|
|
|
* @method Movie[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
22
|
|
|
*/ |
23
|
|
|
class MovieRecommendationRepository extends ServiceEntityRepository |
24
|
|
|
{ |
25
|
|
|
public function __construct(RegistryInterface $registry) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($registry, MovieRecommendation::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function findAllByUser(int $userId, int $minVote = 7): Query |
31
|
|
|
{ |
32
|
|
|
$query = $this->getEntityManager()->createQueryBuilder() |
33
|
|
|
->select('m, COUNT(mr.recommendedMovie) HIDDEN rate') |
34
|
|
|
->from(UserWatchedMovie::class, 'uwm') |
35
|
|
|
->leftJoin(MovieRecommendation::class, 'mr', 'WITH', 'uwm.movie = mr.originalMovie') |
36
|
|
|
->leftJoin(Movie::class, 'm', 'WITH', 'mr.recommendedMovie = m') |
37
|
|
|
->where('uwm.user = :user AND uwm.vote >= :vote') |
38
|
|
|
->setParameter('user', $userId) |
39
|
|
|
->setParameter('vote', $minVote) |
40
|
|
|
->groupBy('mr.recommendedMovie, m.id') |
41
|
|
|
->orderBy('rate', 'DESC') |
42
|
|
|
->getQuery(); |
43
|
|
|
|
44
|
|
|
return $query; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int $movieId |
49
|
|
|
* @param int $userId |
50
|
|
|
* @return array [...[movie_id: int, rate: int, user_id: ?int]} |
51
|
|
|
* @throws \Doctrine\DBAL\DBALException |
52
|
|
|
*/ |
53
|
|
|
public function findAllByMovieAndUser(int $movieId, int $userId): array |
54
|
|
|
{ |
55
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
56
|
|
|
|
57
|
|
|
$sql = 'SELECT DISTINCT ON(mr.recommended_movie_id) mr.recommended_movie_id movie_id, mr.rate, umr.user_id |
58
|
|
|
FROM ( |
59
|
|
|
SELECT mr.recommended_movie_id, COUNT(mr.recommended_movie_id) rate |
60
|
|
|
FROM movies_recommendations mr |
61
|
|
|
WHERE mr.original_movie_id = :movie_id |
62
|
|
|
GROUP BY mr.recommended_movie_id |
63
|
|
|
ORDER BY rate |
64
|
|
|
) mr |
65
|
|
|
LEFT JOIN movies_recommendations umr ON umr.recommended_movie_id = mr.recommended_movie_id AND umr.user_id = :user_id'; |
66
|
|
|
|
67
|
|
|
$statement = $connection->prepare($sql); |
68
|
|
|
$statement->bindValue('movie_id', $movieId); |
69
|
|
|
$statement->bindValue('user_id', $userId); |
70
|
|
|
|
71
|
|
|
$statement->execute(); |
72
|
|
|
|
73
|
|
|
return $statement->fetchAll(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $movieId |
78
|
|
|
* @return array [...[movie_id: int, rate: int]} |
79
|
|
|
* @throws \Doctrine\DBAL\DBALException |
80
|
|
|
*/ |
81
|
|
|
public function findAllByMovie(int $movieId): array |
82
|
|
|
{ |
83
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
84
|
|
|
|
85
|
|
|
$sql = 'SELECT DISTINCT ON(mr.recommended_movie_id) mr.recommended_movie_id movie_id, mr.rate |
86
|
|
|
FROM ( |
87
|
|
|
SELECT mr.recommended_movie_id, COUNT(mr.recommended_movie_id) rate |
88
|
|
|
FROM movies_recommendations mr |
89
|
|
|
WHERE mr.original_movie_id = :movie_id |
90
|
|
|
GROUP BY mr.recommended_movie_id |
91
|
|
|
ORDER BY rate |
92
|
|
|
) mr |
93
|
|
|
'; |
94
|
|
|
|
95
|
|
|
$statement = $connection->prepare($sql); |
96
|
|
|
$statement->bindValue('movie_id', $movieId); |
97
|
|
|
|
98
|
|
|
$statement->execute(); |
99
|
|
|
|
100
|
|
|
return $statement->fetchAll(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|