|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
7
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
|
8
|
|
|
use Doctrine\Common\Collections\Expr\Value; |
|
9
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* VideoRepository. |
|
13
|
|
|
* |
|
14
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
|
15
|
|
|
* repository methods below. |
|
16
|
|
|
*/ |
|
17
|
|
|
class VideoRepository extends \Doctrine\ORM\EntityRepository |
|
18
|
|
|
{ |
|
19
|
|
|
public function findLatest($nth = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$query = $this->getEntityManager()->createQuery( |
|
22
|
|
|
'SELECT v FROM AppBundle:Video v ORDER BY v.updatedAt DESC' |
|
23
|
|
|
); |
|
24
|
|
|
if ($nth) { |
|
25
|
|
|
$query->setMaxResults($nth); |
|
26
|
|
|
} |
|
27
|
|
|
return $query->getResult(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This method performs search. |
|
32
|
|
|
* A queryBuilder is first created and then filters are dynamical applied to this queryBuilder. |
|
33
|
|
|
* |
|
34
|
|
|
* @param $criteria |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function findVideo($criteria) |
|
39
|
|
|
{ |
|
40
|
|
|
$qb = $this->createQueryBuilder('video'); |
|
41
|
|
|
$criteria = array_filter($criteria); //remove fields with no value |
|
42
|
|
|
foreach ($criteria as $field => $value) { |
|
43
|
|
|
if ($value instanceof ArrayCollection) { |
|
44
|
|
|
if ($value->isEmpty()) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
$addFilterMethod = sprintf('%s%s', 'addFilter', ucfirst($field)); //build the dynamic filter to apply on $qb |
|
49
|
|
|
$qb = $this->$addFilterMethod($qb, $criteria[$field]); |
|
50
|
|
|
} |
|
51
|
|
|
$qb->orderBy('video.id', 'DESC'); |
|
52
|
|
|
|
|
53
|
|
|
return $qb->getQuery()->getResult(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* add filter on title field. |
|
58
|
|
|
* |
|
59
|
|
|
* @param QueryBuilder $qb |
|
60
|
|
|
* @param string $title |
|
61
|
|
|
* |
|
62
|
|
|
* @return QueryBuilder |
|
63
|
|
|
*/ |
|
64
|
|
|
private function addFilterTitle(QueryBuilder $qb, $title) |
|
65
|
|
|
{ |
|
66
|
|
|
$criteria = Criteria::create()->andWhere(new Comparison('title', Comparison::CONTAINS, $title)); |
|
67
|
|
|
$qb->addCriteria($criteria); |
|
68
|
|
|
|
|
69
|
|
|
return $qb; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* add filter on tags field. |
|
74
|
|
|
* |
|
75
|
|
|
* @param QueryBuilder $qb |
|
76
|
|
|
* @param ArrayCollection $tags |
|
77
|
|
|
* |
|
78
|
|
|
* @return QueryBuilder |
|
79
|
|
|
*/ |
|
80
|
|
|
private function addFilterTags(QueryBuilder $qb, ArrayCollection $tags) |
|
81
|
|
|
{ |
|
82
|
|
|
$qb->innerJoin('video.tags', 'tags'); |
|
83
|
|
|
$criteria = Criteria::create()->andWhere(new Comparison('tags', Comparison::IN, new Value($tags))); |
|
84
|
|
|
$qb->addCriteria($criteria); |
|
85
|
|
|
|
|
86
|
|
|
return $qb; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function addFilterDescription(QueryBuilder $qb, $description) |
|
90
|
|
|
{ |
|
91
|
|
|
$criteria = Criteria::create()->andWhere(new Comparison('description', Comparison::CONTAINS, $description)); |
|
92
|
|
|
$qb->addCriteria($criteria); |
|
93
|
|
|
|
|
94
|
|
|
return $qb; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|