Conditions | 10 |
Paths | 135 |
Total Lines | 67 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | private function getFixedQueryBuilder(QueryBuilder $queryBuilder) |
||
73 | { |
||
74 | $queryBuilderId = clone $queryBuilder; |
||
75 | |||
76 | // step 1 : retrieve the targeted class |
||
77 | $from = $queryBuilderId->getDQLPart('from'); |
||
78 | $class = $from[0]->getFrom(); |
||
79 | $metadata = $queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class); |
||
80 | |||
81 | // step 2 : retrieve identifier columns |
||
82 | $idNames = $metadata->getIdentifierFieldNames(); |
||
83 | |||
84 | // step 3 : retrieve the different subjects ids |
||
85 | $selects = array(); |
||
86 | $idxSelect = ''; |
||
87 | foreach ($idNames as $idName) { |
||
88 | $select = sprintf('%s.%s', $queryBuilderId->getRootAlias(), $idName); |
||
|
|||
89 | // Put the ID select on this array to use it on results QB |
||
90 | $selects[$idName] = $select; |
||
91 | // Use IDENTITY if id is a relation too. See: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html |
||
92 | // Should work only with doctrine/orm: ~2.2 |
||
93 | $idSelect = $select; |
||
94 | if ($metadata->hasAssociation($idName)) { |
||
95 | $idSelect = sprintf('IDENTITY(%s) as %s', $idSelect, $idName); |
||
96 | } |
||
97 | $idxSelect .= ($idxSelect !== '' ? ', ' : '') . $idSelect; |
||
98 | } |
||
99 | $queryBuilderId->resetDQLPart('select'); |
||
100 | $queryBuilderId->add('select', 'DISTINCT '.$idxSelect); |
||
101 | |||
102 | // for SELECT DISTINCT, ORDER BY expressions must appear in idxSelect list |
||
103 | /* Consider |
||
104 | SELECT DISTINCT x FROM tab ORDER BY y; |
||
105 | For any particular x-value in the table there might be many different y |
||
106 | values. Which one will you use to sort that x-value in the output? |
||
107 | */ |
||
108 | // todo : check how doctrine behave, potential SQL injection here ... |
||
109 | if ($this->getSortBy()) { |
||
110 | $sortBy = $this->getSortBy(); |
||
111 | if (strpos($sortBy, '.') === false) { // add the current alias |
||
112 | $sortBy = $queryBuilderId->getRootAlias() . '.' . $sortBy; |
||
113 | } |
||
114 | $sortBy .= ' AS __order_by'; |
||
115 | $queryBuilderId->addSelect($sortBy); |
||
116 | } |
||
117 | |||
118 | $results = $queryBuilderId->getQuery()->execute(array(), Query::HYDRATE_ARRAY); |
||
119 | $idxMatrix = array(); |
||
120 | foreach ($results as $id) { |
||
121 | foreach ($idNames as $idName) { |
||
122 | $idxMatrix[$idName][] = $id[$idName]; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // step 4 : alter the query to match the targeted ids |
||
127 | foreach ($idxMatrix as $idName => $idx) { |
||
128 | if (count($idx) > 0) { |
||
129 | $idxParamName = sprintf('%s_idx', $idName); |
||
130 | $queryBuilder->andWhere(sprintf('%s IN (:%s)', $selects[$idName], $idxParamName)); |
||
131 | $queryBuilder->setParameter($idxParamName, $idx); |
||
132 | $queryBuilder->setMaxResults(null); |
||
133 | $queryBuilder->setFirstResult(null); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | return $queryBuilder; |
||
138 | } |
||
139 | |||
285 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.