1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Rafidion Michael |
5
|
|
|
* Date: 30/11/2014 |
6
|
|
|
* Time: 03:30 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Mykees\MediaBundle\Repository; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityRepository; |
13
|
|
|
|
14
|
|
|
class MediaRepository extends EntityRepository |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Get the query by model name and model id |
18
|
|
|
* @param $model |
19
|
|
|
* @param $model_id |
20
|
|
|
* @return \Doctrine\ORM\Query |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function queryForModelAndId($model, $model_id) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$query = $this->_em->createQueryBuilder() |
25
|
|
|
->select('m') |
26
|
|
|
->from($this->_entityName,'m') |
27
|
|
|
->where('m.model = :model') |
28
|
|
|
->andWhere('m.modelId = :modelId') |
29
|
|
|
->setParameter('model',$model) |
30
|
|
|
->setParameter('modelId',$model_id) |
31
|
|
|
->getQuery() |
32
|
|
|
; |
33
|
|
|
return $query; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get query for model name |
38
|
|
|
* @param $model |
39
|
|
|
* @return \Doctrine\ORM\Query |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function queryForModel($model) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$query = $this->_em->createQueryBuilder() |
44
|
|
|
->select('m') |
45
|
|
|
->from($this->_entityName,'m') |
46
|
|
|
->where('m.model = :model') |
47
|
|
|
->setParameter('model',$model) |
48
|
|
|
->getQuery() |
49
|
|
|
; |
50
|
|
|
return $query; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* get query for an array objects |
55
|
|
|
* @param array $model_info |
56
|
|
|
* @return \Doctrine\ORM\Query |
57
|
|
|
*/ |
58
|
|
|
public function queryForArray(array $model_info) |
59
|
|
|
{ |
60
|
|
|
$query = $this->_em->createQueryBuilder() |
61
|
|
|
->select('m') |
62
|
|
|
->from($this->_entityName,'m') |
63
|
|
|
->where('m.model IN(:model)') |
64
|
|
|
->andWhere('m.modelId IN(:modelId)') |
65
|
|
|
->setParameter('model',array_values($model_info['models'])) |
66
|
|
|
->setParameter('modelId',array_values($model_info['ids'])) |
67
|
|
|
->getQuery() |
68
|
|
|
; |
69
|
|
|
return $query; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* find media for an array objects |
74
|
|
|
* @param array $model_info |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function findForArrayModels(array $model_info){ |
78
|
|
|
return $this->queryForArray($model_info) |
79
|
|
|
->getResult(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* find media by model name and model id |
84
|
|
|
* @param $model |
85
|
|
|
* @param $model_id |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function findForModelAndId($model, $model_id) |
89
|
|
|
{ |
90
|
|
|
return $this->queryForModelAndId($model, $model_id) |
91
|
|
|
->getResult(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get media by model name |
96
|
|
|
* @param $model |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function findForModel($model) |
100
|
|
|
{ |
101
|
|
|
return $this->queryForModel($model) |
102
|
|
|
->getResult(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.