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
|
|
|
/** |
30
|
|
|
* @param int $movieId |
31
|
|
|
* @param int $userId |
32
|
|
|
* @return array [...[movie_id: int, rate: int, user_id: ?int]} |
33
|
|
|
* @throws \Doctrine\DBAL\DBALException |
34
|
|
|
*/ |
35
|
|
|
public function findAllByMovieAndUser(int $movieId, int $userId): array |
36
|
|
|
{ |
37
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
38
|
|
|
|
39
|
|
|
$sql = 'SELECT DISTINCT ON(mr.recommended_movie_id) mr.recommended_movie_id movie_id, mr.rate, umr.user_id |
40
|
|
|
FROM ( |
41
|
|
|
SELECT mr.recommended_movie_id, COUNT(mr.recommended_movie_id) rate |
42
|
|
|
FROM movies_recommendations mr |
43
|
|
|
WHERE mr.original_movie_id = :movie_id |
44
|
|
|
GROUP BY mr.recommended_movie_id |
45
|
|
|
ORDER BY rate |
46
|
|
|
) mr |
47
|
|
|
LEFT JOIN movies_recommendations umr ON umr.recommended_movie_id = mr.recommended_movie_id AND umr.user_id = :user_id'; |
48
|
|
|
|
49
|
|
|
$statement = $connection->prepare($sql); |
50
|
|
|
$statement->bindValue('movie_id', $movieId); |
51
|
|
|
$statement->bindValue('user_id', $userId); |
52
|
|
|
|
53
|
|
|
$statement->execute(); |
54
|
|
|
|
55
|
|
|
return $statement->fetchAll(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $movieId |
60
|
|
|
* @return array [...[movie_id: int, rate: int]} |
61
|
|
|
* @throws \Doctrine\DBAL\DBALException |
62
|
|
|
*/ |
63
|
|
|
public function findAllByMovie(int $movieId): array |
64
|
|
|
{ |
65
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
66
|
|
|
|
67
|
|
|
$sql = 'SELECT DISTINCT ON(mr.recommended_movie_id) mr.recommended_movie_id movie_id, mr.rate |
68
|
|
|
FROM ( |
69
|
|
|
SELECT mr.recommended_movie_id, COUNT(mr.recommended_movie_id) rate |
70
|
|
|
FROM movies_recommendations mr |
71
|
|
|
WHERE mr.original_movie_id = :movie_id |
72
|
|
|
GROUP BY mr.recommended_movie_id |
73
|
|
|
ORDER BY rate |
74
|
|
|
) mr |
75
|
|
|
'; |
76
|
|
|
|
77
|
|
|
$statement = $connection->prepare($sql); |
78
|
|
|
$statement->bindValue('movie_id', $movieId); |
79
|
|
|
|
80
|
|
|
$statement->execute(); |
81
|
|
|
|
82
|
|
|
return $statement->fetchAll(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|