|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Wander; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
9
|
|
|
use Doctrine\ORM\Query; |
|
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
11
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method Wander|null find($id, $lockMode = null, $lockVersion = null) |
|
15
|
|
|
* @method Wander|null findOneBy(array $criteria, array $orderBy = null) |
|
16
|
|
|
* @method Wander[] findAll() |
|
17
|
|
|
* @method Wander[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
18
|
|
|
*/ |
|
19
|
|
|
class WanderRepository extends ServiceEntityRepository |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct(ManagerRegistry $registry) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($registry, Wander::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function findAll() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->findBy(array(), array('startTime' => 'DESC')); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function standardQueryBuilder(): QueryBuilder |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->createQueryBuilder('w') |
|
34
|
|
|
->orderBy('w.startTime', 'desc'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function wandersWithImageCountQueryBuilder(): QueryBuilder |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->standardQueryBuilder() |
|
40
|
|
|
->select('w as wander') |
|
41
|
|
|
// TODO: Is there a better way to do this without namespacing? This is *fast* compared |
|
42
|
|
|
// to a grouped query and works fine, though. |
|
43
|
|
|
->addSelect('(SELECT COUNT(i) FROM App\Entity\Image i WHERE i.wander = w) AS imageCount'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function addWhereHasImages(QueryBuilder $qb, ?bool $hasImages = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($hasImages !== null) { |
|
49
|
|
|
return $qb->andWhere('w.images is ' . ($hasImages ? 'not' : '') . ' empty'); |
|
50
|
|
|
} |
|
51
|
|
|
return $qb; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Used by Elastica to transform results to model |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $entityAlias |
|
58
|
|
|
* @return QueryBuilder |
|
59
|
|
|
*/ |
|
60
|
|
|
public function createSearchQueryBuilder(string $entityAlias): QueryBuilder { |
|
61
|
|
|
$qb = $this->createQueryBuilder($entityAlias); |
|
62
|
|
|
$qb->select($entityAlias, 'i') |
|
63
|
|
|
->leftJoin($entityAlias.'.images', 'i'); |
|
64
|
|
|
return $qb; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// TODO: Do we still use this? We might only be using findFirstWhereIncludesDate now |
|
68
|
|
|
public function findWhereIncludesDate(DateTimeInterface $target) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->createQueryBuilder('w') |
|
71
|
|
|
->andWhere(':target BETWEEN w.startTime AND w.endTime') |
|
72
|
|
|
->setParameter('target', $target) |
|
73
|
|
|
->orderBy('w.startTime') |
|
74
|
|
|
->getQuery() |
|
75
|
|
|
->getResult(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function findFirstWhereIncludesDate(DateTimeInterface $target) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->createQueryBuilder('w') |
|
81
|
|
|
->andWhere(':target BETWEEN w.startTime AND w.endTime') |
|
82
|
|
|
->setParameter('target', $target) |
|
83
|
|
|
->orderBy('w.startTime') |
|
84
|
|
|
->setMaxResults(1) |
|
85
|
|
|
->getQuery() |
|
86
|
|
|
->getOneOrNullResult(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function findShortest() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->createQueryBuilder('w') |
|
92
|
|
|
->select('w.id, w.distance') |
|
93
|
|
|
->orderBy('w.distance', 'asc') |
|
94
|
|
|
->setMaxResults(1) |
|
95
|
|
|
->getQuery() |
|
96
|
|
|
->getOneOrNullResult(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function findLongest() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->createQueryBuilder('w') |
|
102
|
|
|
->select('w.id, w.distance') |
|
103
|
|
|
->orderBy('w.distance', 'desc') |
|
104
|
|
|
->setMaxResults(1) |
|
105
|
|
|
->getQuery() |
|
106
|
|
|
->getOneOrNullResult(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function findAverageDistance() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->createQueryBuilder('w') |
|
112
|
|
|
->select('AVG(w.distance)') |
|
113
|
|
|
->getQuery() |
|
114
|
|
|
->getSingleScalarResult() ?? 0; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function findNext(Wander $wander): ?Wander |
|
118
|
|
|
{ |
|
119
|
|
|
$qb = $this->createQueryBuilder('w'); |
|
120
|
|
|
return $qb->add('where', $qb->expr()->gt('w.startTime', ':startTime')) |
|
121
|
|
|
->setParameter('startTime', $wander->getStartTime()) |
|
122
|
|
|
->addOrderBy('w.startTime') |
|
123
|
|
|
->setMaxResults(1) |
|
124
|
|
|
->getQuery() |
|
125
|
|
|
->getOneOrNullResult(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function findPrev(Wander $wander): ?Wander |
|
129
|
|
|
{ |
|
130
|
|
|
$qb = $this->createQueryBuilder('w'); |
|
131
|
|
|
return $qb->add('where', $qb->expr()->lt('w.startTime', ':startTime')) |
|
132
|
|
|
->setParameter('startTime', $wander->getStartTime()) |
|
133
|
|
|
->addOrderBy('w.startTime', 'desc') |
|
134
|
|
|
->setMaxResults(1) |
|
135
|
|
|
->getQuery() |
|
136
|
|
|
->getOneOrNullResult(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
// /** |
|
141
|
|
|
// * @return Wander[] Returns an array of Wander objects |
|
142
|
|
|
// */ |
|
143
|
|
|
/* |
|
144
|
|
|
public function findByExampleField($value) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->createQueryBuilder('w') |
|
147
|
|
|
->andWhere('w.exampleField = :val') |
|
148
|
|
|
->setParameter('val', $value) |
|
149
|
|
|
->orderBy('w.id', 'ASC') |
|
150
|
|
|
->setMaxResults(10) |
|
151
|
|
|
->getQuery() |
|
152
|
|
|
->getResult() |
|
153
|
|
|
; |
|
154
|
|
|
} |
|
155
|
|
|
*/ |
|
156
|
|
|
|
|
157
|
|
|
/* |
|
158
|
|
|
public function findOneBySomeField($value): ?Wander |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->createQueryBuilder('w') |
|
161
|
|
|
->andWhere('w.exampleField = :val') |
|
162
|
|
|
->setParameter('val', $value) |
|
163
|
|
|
->getQuery() |
|
164
|
|
|
->getOneOrNullResult() |
|
165
|
|
|
; |
|
166
|
|
|
} |
|
167
|
|
|
*/ |
|
168
|
|
|
} |
|
169
|
|
|
|