Complex classes like QueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class QueryBuilder |
||
38 | { |
||
39 | /* The query types. */ |
||
40 | const SELECT = 0; |
||
41 | const DELETE = 1; |
||
42 | const UPDATE = 2; |
||
43 | |||
44 | /* The builder states. */ |
||
45 | const STATE_DIRTY = 0; |
||
46 | const STATE_CLEAN = 1; |
||
47 | |||
48 | /** |
||
49 | * The EntityManager used by this QueryBuilder. |
||
50 | * |
||
51 | * @var EntityManagerInterface |
||
52 | */ |
||
53 | private $_em; |
||
54 | |||
55 | /** |
||
56 | * The array of DQL parts collected. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $_dqlParts = array( |
||
61 | 'distinct' => false, |
||
62 | 'select' => array(), |
||
63 | 'from' => array(), |
||
64 | 'join' => array(), |
||
65 | 'set' => array(), |
||
66 | 'where' => null, |
||
67 | 'groupBy' => array(), |
||
68 | 'having' => null, |
||
69 | 'orderBy' => array() |
||
70 | ); |
||
71 | |||
72 | /** |
||
73 | * The type of query this is. Can be select, update or delete. |
||
74 | * |
||
75 | * @var integer |
||
76 | */ |
||
77 | private $_type = self::SELECT; |
||
78 | |||
79 | /** |
||
80 | * The state of the query object. Can be dirty or clean. |
||
81 | * |
||
82 | * @var integer |
||
83 | */ |
||
84 | private $_state = self::STATE_CLEAN; |
||
85 | |||
86 | /** |
||
87 | * The complete DQL string for this query. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $_dql; |
||
92 | |||
93 | /** |
||
94 | * The query parameters. |
||
95 | * |
||
96 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
97 | */ |
||
98 | private $parameters; |
||
99 | |||
100 | /** |
||
101 | * The index of the first result to retrieve. |
||
102 | * |
||
103 | * @var integer |
||
104 | */ |
||
105 | private $_firstResult = null; |
||
106 | |||
107 | /** |
||
108 | * The maximum number of results to retrieve. |
||
109 | * |
||
110 | * @var integer |
||
111 | */ |
||
112 | private $_maxResults = null; |
||
113 | |||
114 | /** |
||
115 | * Keeps root entity alias names for join entities. |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | private $joinRootAliases = array(); |
||
120 | |||
121 | /** |
||
122 | * Whether to use second level cache, if available. |
||
123 | * |
||
124 | * @var boolean |
||
125 | */ |
||
126 | protected $cacheable = false; |
||
127 | |||
128 | /** |
||
129 | * Second level cache region name. |
||
130 | * |
||
131 | * @var string|null |
||
132 | */ |
||
133 | protected $cacheRegion; |
||
134 | |||
135 | /** |
||
136 | * Second level query cache mode. |
||
137 | * |
||
138 | * @var integer|null |
||
139 | */ |
||
140 | protected $cacheMode; |
||
141 | |||
142 | /** |
||
143 | * @var integer |
||
144 | */ |
||
145 | protected $lifetime = 0; |
||
146 | |||
147 | /** |
||
148 | * Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>. |
||
149 | * |
||
150 | * @param EntityManagerInterface $em The EntityManager to use. |
||
151 | */ |
||
152 | 113 | public function __construct(EntityManagerInterface $em) |
|
157 | |||
158 | /** |
||
159 | * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
||
160 | * This producer method is intended for convenient inline usage. Example: |
||
161 | * |
||
162 | * <code> |
||
163 | * $qb = $em->createQueryBuilder(); |
||
164 | * $qb |
||
165 | * ->select('u') |
||
166 | * ->from('User', 'u') |
||
167 | * ->where($qb->expr()->eq('u.id', 1)); |
||
168 | * </code> |
||
169 | * |
||
170 | * For more complex expression construction, consider storing the expression |
||
171 | * builder object in a local variable. |
||
172 | * |
||
173 | * @return Query\Expr |
||
174 | */ |
||
175 | 11 | public function expr() |
|
179 | |||
180 | /** |
||
181 | * |
||
182 | * Enable/disable second level query (result) caching for this query. |
||
183 | * |
||
184 | * @param boolean $cacheable |
||
185 | * |
||
186 | * @return self |
||
187 | */ |
||
188 | 1 | public function setCacheable($cacheable) |
|
194 | |||
195 | /** |
||
196 | * @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise. |
||
197 | */ |
||
198 | 1 | public function isCacheable() |
|
202 | |||
203 | /** |
||
204 | * @param string $cacheRegion |
||
205 | * |
||
206 | * @return self |
||
207 | */ |
||
208 | 1 | public function setCacheRegion($cacheRegion) |
|
214 | |||
215 | /** |
||
216 | * Obtain the name of the second level query cache region in which query results will be stored |
||
217 | * |
||
218 | * @return string|null The cache region name; NULL indicates the default region. |
||
219 | */ |
||
220 | 1 | public function getCacheRegion() |
|
224 | |||
225 | /** |
||
226 | * @return integer |
||
227 | */ |
||
228 | 1 | public function getLifetime() |
|
232 | |||
233 | /** |
||
234 | * Sets the life-time for this query into second level cache. |
||
235 | * |
||
236 | * @param integer $lifetime |
||
237 | * |
||
238 | * @return self |
||
239 | */ |
||
240 | 1 | public function setLifetime($lifetime) |
|
246 | |||
247 | /** |
||
248 | * @return integer |
||
249 | */ |
||
250 | 1 | public function getCacheMode() |
|
254 | |||
255 | /** |
||
256 | * @param integer $cacheMode |
||
257 | * |
||
258 | * @return self |
||
259 | */ |
||
260 | 1 | public function setCacheMode($cacheMode) |
|
266 | |||
267 | /** |
||
268 | * Gets the type of the currently built query. |
||
269 | * |
||
270 | * @return integer |
||
271 | */ |
||
272 | 4 | public function getType() |
|
276 | |||
277 | /** |
||
278 | * Gets the associated EntityManager for this query builder. |
||
279 | * |
||
280 | * @return EntityManager |
||
281 | */ |
||
282 | 1 | public function getEntityManager() |
|
286 | |||
287 | /** |
||
288 | * Gets the state of this query builder instance. |
||
289 | * |
||
290 | * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
||
291 | */ |
||
292 | 2 | public function getState() |
|
296 | |||
297 | /** |
||
298 | * Gets the complete DQL string formed by the current specifications of this QueryBuilder. |
||
299 | * |
||
300 | * <code> |
||
301 | * $qb = $em->createQueryBuilder() |
||
302 | * ->select('u') |
||
303 | * ->from('User', 'u'); |
||
304 | * echo $qb->getDql(); // SELECT u FROM User u |
||
305 | * </code> |
||
306 | * |
||
307 | * @return string The DQL query string. |
||
308 | */ |
||
309 | 77 | public function getDQL() |
|
335 | |||
336 | /** |
||
337 | * Constructs a Query instance from the current specifications of the builder. |
||
338 | * |
||
339 | * <code> |
||
340 | * $qb = $em->createQueryBuilder() |
||
341 | * ->select('u') |
||
342 | * ->from('User', 'u'); |
||
343 | * $q = $qb->getQuery(); |
||
344 | * $results = $q->execute(); |
||
345 | * </code> |
||
346 | * |
||
347 | * @return Query |
||
348 | */ |
||
349 | 66 | public function getQuery() |
|
375 | |||
376 | /** |
||
377 | * Finds the root entity alias of the joined entity. |
||
378 | * |
||
379 | * @param string $alias The alias of the new join entity |
||
380 | * @param string $parentAlias The parent entity alias of the join relationship |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 27 | private function findRootAlias($alias, $parentAlias) |
|
402 | |||
403 | /** |
||
404 | * Gets the FIRST root alias of the query. This is the first entity alias involved |
||
405 | * in the construction of the query. |
||
406 | * |
||
407 | * <code> |
||
408 | * $qb = $em->createQueryBuilder() |
||
409 | * ->select('u') |
||
410 | * ->from('User', 'u'); |
||
411 | * |
||
412 | * echo $qb->getRootAlias(); // u |
||
413 | * </code> |
||
414 | * |
||
415 | * @deprecated Please use $qb->getRootAliases() instead. |
||
416 | * @throws \RuntimeException |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | 3 | public function getRootAlias() |
|
421 | { |
||
422 | 3 | $aliases = $this->getRootAliases(); |
|
423 | |||
424 | 3 | if ( ! isset($aliases[0])) { |
|
425 | throw new \RuntimeException('No alias was set before invoking getRootAlias().'); |
||
426 | } |
||
427 | |||
428 | 3 | return $aliases[0]; |
|
429 | } |
||
430 | |||
431 | /** |
||
432 | * Gets the root aliases of the query. This is the entity aliases involved |
||
433 | * in the construction of the query. |
||
434 | * |
||
435 | * <code> |
||
436 | * $qb = $em->createQueryBuilder() |
||
437 | * ->select('u') |
||
438 | * ->from('User', 'u'); |
||
439 | * |
||
440 | * $qb->getRootAliases(); // array('u') |
||
441 | * </code> |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | 43 | public function getRootAliases() |
|
463 | |||
464 | /** |
||
465 | * Gets all the aliases that have been used in the query. |
||
466 | * Including all select root aliases and join aliases |
||
467 | * |
||
468 | * <code> |
||
469 | * $qb = $em->createQueryBuilder() |
||
470 | * ->select('u') |
||
471 | * ->from('User', 'u') |
||
472 | * ->join('u.articles','a'; |
||
473 | * |
||
474 | * $qb->getAllAliases(); // array('u','a') |
||
475 | * </code> |
||
476 | * @return array |
||
477 | */ |
||
478 | 15 | public function getAllAliases() { |
|
481 | |||
482 | /** |
||
483 | * Gets the root entities of the query. This is the entity aliases involved |
||
484 | * in the construction of the query. |
||
485 | * |
||
486 | * <code> |
||
487 | * $qb = $em->createQueryBuilder() |
||
488 | * ->select('u') |
||
489 | * ->from('User', 'u'); |
||
490 | * |
||
491 | * $qb->getRootEntities(); // array('User') |
||
492 | * </code> |
||
493 | * |
||
494 | * @return array |
||
495 | */ |
||
496 | 1 | public function getRootEntities() |
|
514 | |||
515 | /** |
||
516 | * Sets a query parameter for the query being constructed. |
||
517 | * |
||
518 | * <code> |
||
519 | * $qb = $em->createQueryBuilder() |
||
520 | * ->select('u') |
||
521 | * ->from('User', 'u') |
||
522 | * ->where('u.id = :user_id') |
||
523 | * ->setParameter('user_id', 1); |
||
524 | * </code> |
||
525 | * |
||
526 | * @param string|integer $key The parameter position or name. |
||
527 | * @param mixed $value The parameter value. |
||
528 | * @param string|null $type PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constant |
||
529 | * |
||
530 | * @return self |
||
531 | */ |
||
532 | 8 | public function setParameter($key, $value, $type = null) |
|
557 | |||
558 | /** |
||
559 | * Sets a collection of query parameters for the query being constructed. |
||
560 | * |
||
561 | * <code> |
||
562 | * $qb = $em->createQueryBuilder() |
||
563 | * ->select('u') |
||
564 | * ->from('User', 'u') |
||
565 | * ->where('u.id = :user_id1 OR u.id = :user_id2') |
||
566 | * ->setParameters(new ArrayCollection(array( |
||
567 | * new Parameter('user_id1', 1), |
||
568 | * new Parameter('user_id2', 2) |
||
569 | * ))); |
||
570 | * </code> |
||
571 | * |
||
572 | * @param \Doctrine\Common\Collections\ArrayCollection|array $parameters The query parameters to set. |
||
573 | * |
||
574 | * @return self |
||
575 | */ |
||
576 | 4 | public function setParameters($parameters) |
|
595 | |||
596 | /** |
||
597 | * Gets all defined query parameters for the query being constructed. |
||
598 | * |
||
599 | * @return \Doctrine\Common\Collections\ArrayCollection The currently defined query parameters. |
||
600 | */ |
||
601 | 2 | public function getParameters() |
|
605 | |||
606 | /** |
||
607 | * Gets a (previously set) query parameter of the query being constructed. |
||
608 | * |
||
609 | * @param mixed $key The key (index or name) of the bound parameter. |
||
610 | * |
||
611 | * @return Query\Parameter|null The value of the bound parameter. |
||
612 | */ |
||
613 | 12 | public function getParameter($key) |
|
626 | |||
627 | /** |
||
628 | * Sets the position of the first result to retrieve (the "offset"). |
||
629 | * |
||
630 | * @param integer $firstResult The first result to return. |
||
631 | * |
||
632 | * @return self |
||
633 | */ |
||
634 | 2 | public function setFirstResult($firstResult) |
|
640 | |||
641 | /** |
||
642 | * Gets the position of the first result the query object was set to retrieve (the "offset"). |
||
643 | * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
||
644 | * |
||
645 | * @return integer The position of the first result. |
||
646 | */ |
||
647 | 2 | public function getFirstResult() |
|
651 | |||
652 | /** |
||
653 | * Sets the maximum number of results to retrieve (the "limit"). |
||
654 | * |
||
655 | * @param integer $maxResults The maximum number of results to retrieve. |
||
656 | * |
||
657 | * @return self |
||
658 | */ |
||
659 | 2 | public function setMaxResults($maxResults) |
|
665 | |||
666 | /** |
||
667 | * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
||
668 | * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
||
669 | * |
||
670 | * @return integer Maximum number of results. |
||
671 | */ |
||
672 | 2 | public function getMaxResults() |
|
676 | |||
677 | /** |
||
678 | * Either appends to or replaces a single, generic query part. |
||
679 | * |
||
680 | * The available parts are: 'select', 'from', 'join', 'set', 'where', |
||
681 | * 'groupBy', 'having' and 'orderBy'. |
||
682 | * |
||
683 | * @param string $dqlPartName The DQL part name. |
||
684 | * @param object|array $dqlPart An Expr object. |
||
685 | * @param bool $append Whether to append (true) or replace (false). |
||
686 | * |
||
687 | * @return self |
||
688 | */ |
||
689 | 109 | public function add($dqlPartName, $dqlPart, $append = false) |
|
736 | |||
737 | /** |
||
738 | * Specifies an item that is to be returned in the query result. |
||
739 | * Replaces any previously specified selections, if any. |
||
740 | * |
||
741 | * <code> |
||
742 | * $qb = $em->createQueryBuilder() |
||
743 | * ->select('u', 'p') |
||
744 | * ->from('User', 'u') |
||
745 | * ->leftJoin('u.Phonenumbers', 'p'); |
||
746 | * </code> |
||
747 | * |
||
748 | * @param mixed $select The selection expressions. |
||
749 | * |
||
750 | * @return self |
||
751 | */ |
||
752 | 102 | public function select($select = null) |
|
764 | |||
765 | /** |
||
766 | * Adds a DISTINCT flag to this query. |
||
767 | * |
||
768 | * <code> |
||
769 | * $qb = $em->createQueryBuilder() |
||
770 | * ->select('u') |
||
771 | * ->distinct() |
||
772 | * ->from('User', 'u'); |
||
773 | * </code> |
||
774 | * |
||
775 | * @param bool $flag |
||
776 | * |
||
777 | * @return self |
||
778 | */ |
||
779 | 1 | public function distinct($flag = true) |
|
785 | |||
786 | /** |
||
787 | * Adds an item that is to be returned in the query result. |
||
788 | * |
||
789 | * <code> |
||
790 | * $qb = $em->createQueryBuilder() |
||
791 | * ->select('u') |
||
792 | * ->addSelect('p') |
||
793 | * ->from('User', 'u') |
||
794 | * ->leftJoin('u.Phonenumbers', 'p'); |
||
795 | * </code> |
||
796 | * |
||
797 | * @param mixed $select The selection expression. |
||
798 | * |
||
799 | * @return self |
||
800 | */ |
||
801 | 1 | public function addSelect($select = null) |
|
813 | |||
814 | /** |
||
815 | * Turns the query being built into a bulk delete query that ranges over |
||
816 | * a certain entity type. |
||
817 | * |
||
818 | * <code> |
||
819 | * $qb = $em->createQueryBuilder() |
||
820 | * ->delete('User', 'u') |
||
821 | * ->where('u.id = :user_id') |
||
822 | * ->setParameter('user_id', 1); |
||
823 | * </code> |
||
824 | * |
||
825 | * @param string $delete The class/type whose instances are subject to the deletion. |
||
826 | * @param string $alias The class/type alias used in the constructed query. |
||
827 | * |
||
828 | * @return self |
||
829 | */ |
||
830 | 4 | public function delete($delete = null, $alias = null) |
|
840 | |||
841 | /** |
||
842 | * Turns the query being built into a bulk update query that ranges over |
||
843 | * a certain entity type. |
||
844 | * |
||
845 | * <code> |
||
846 | * $qb = $em->createQueryBuilder() |
||
847 | * ->update('User', 'u') |
||
848 | * ->set('u.password', '?1') |
||
849 | * ->where('u.id = ?2'); |
||
850 | * </code> |
||
851 | * |
||
852 | * @param string $update The class/type whose instances are subject to the update. |
||
853 | * @param string $alias The class/type alias used in the constructed query. |
||
854 | * |
||
855 | * @return self |
||
856 | */ |
||
857 | 3 | public function update($update = null, $alias = null) |
|
867 | |||
868 | /** |
||
869 | * Creates and adds a query root corresponding to the entity identified by the given alias, |
||
870 | * forming a cartesian product with any existing query roots. |
||
871 | * |
||
872 | * <code> |
||
873 | * $qb = $em->createQueryBuilder() |
||
874 | * ->select('u') |
||
875 | * ->from('User', 'u'); |
||
876 | * </code> |
||
877 | * |
||
878 | * @param string $from The class name. |
||
879 | * @param string $alias The alias of the class. |
||
880 | * @param string $indexBy The index for the from. |
||
881 | * |
||
882 | * @return self |
||
883 | */ |
||
884 | 101 | public function from($from, $alias, $indexBy = null) |
|
888 | |||
889 | /** |
||
890 | * Updates a query root corresponding to an entity setting its index by. This method is intended to be used with |
||
891 | * EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it |
||
892 | * setting an index by. |
||
893 | * |
||
894 | * <code> |
||
895 | * $qb = $userRepository->createQueryBuilder('u') |
||
896 | * ->indexBy('u', 'u.id'); |
||
897 | * |
||
898 | * // Is equivalent to... |
||
899 | * |
||
900 | * $qb = $em->createQueryBuilder() |
||
901 | * ->select('u') |
||
902 | * ->from('User', 'u', 'u.id'); |
||
903 | * </code> |
||
904 | * |
||
905 | * @param string $alias The root alias of the class. |
||
906 | * @param string $indexBy The index for the from. |
||
907 | * |
||
908 | * @return self |
||
909 | * |
||
910 | * @throws Query\QueryException |
||
911 | */ |
||
912 | 2 | public function indexBy($alias, $indexBy) |
|
933 | |||
934 | /** |
||
935 | * Creates and adds a join over an entity association to the query. |
||
936 | * |
||
937 | * The entities in the joined association will be fetched as part of the query |
||
938 | * result if the alias used for the joined association is placed in the select |
||
939 | * expressions. |
||
940 | * |
||
941 | * <code> |
||
942 | * $qb = $em->createQueryBuilder() |
||
943 | * ->select('u') |
||
944 | * ->from('User', 'u') |
||
945 | * ->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
946 | * </code> |
||
947 | * |
||
948 | * @param string $join The relationship to join. |
||
949 | * @param string $alias The alias of the join. |
||
950 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
951 | * @param string|null $condition The condition for the join. |
||
952 | * @param string|null $indexBy The index for the join. |
||
953 | * |
||
954 | * @return self |
||
955 | */ |
||
956 | 8 | public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
960 | |||
961 | /** |
||
962 | * Creates and adds a join over an entity association to the query. |
||
963 | * |
||
964 | * The entities in the joined association will be fetched as part of the query |
||
965 | * result if the alias used for the joined association is placed in the select |
||
966 | * expressions. |
||
967 | * |
||
968 | * [php] |
||
969 | * $qb = $em->createQueryBuilder() |
||
970 | * ->select('u') |
||
971 | * ->from('User', 'u') |
||
972 | * ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
973 | * |
||
974 | * @param string $join The relationship to join. |
||
975 | * @param string $alias The alias of the join. |
||
976 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
977 | * @param string|null $condition The condition for the join. |
||
978 | * @param string|null $indexBy The index for the join. |
||
979 | * |
||
980 | * @return self |
||
981 | */ |
||
982 | 15 | public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
994 | |||
995 | /** |
||
996 | * Creates and adds a left join over an entity association to the query. |
||
997 | * |
||
998 | * The entities in the joined association will be fetched as part of the query |
||
999 | * result if the alias used for the joined association is placed in the select |
||
1000 | * expressions. |
||
1001 | * |
||
1002 | * <code> |
||
1003 | * $qb = $em->createQueryBuilder() |
||
1004 | * ->select('u') |
||
1005 | * ->from('User', 'u') |
||
1006 | * ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
1007 | * </code> |
||
1008 | * |
||
1009 | * @param string $join The relationship to join. |
||
1010 | * @param string $alias The alias of the join. |
||
1011 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
1012 | * @param string|null $condition The condition for the join. |
||
1013 | * @param string|null $indexBy The index for the join. |
||
1014 | * |
||
1015 | * @return self |
||
1016 | */ |
||
1017 | 13 | public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
1029 | |||
1030 | /** |
||
1031 | * Sets a new value for a field in a bulk update query. |
||
1032 | * |
||
1033 | * <code> |
||
1034 | * $qb = $em->createQueryBuilder() |
||
1035 | * ->update('User', 'u') |
||
1036 | * ->set('u.password', '?1') |
||
1037 | * ->where('u.id = ?2'); |
||
1038 | * </code> |
||
1039 | * |
||
1040 | * @param string $key The key/field to set. |
||
1041 | * @param string $value The value, expression, placeholder, etc. |
||
1042 | * |
||
1043 | * @return self |
||
1044 | */ |
||
1045 | 2 | public function set($key, $value) |
|
1049 | |||
1050 | /** |
||
1051 | * Specifies one or more restrictions to the query result. |
||
1052 | * Replaces any previously specified restrictions, if any. |
||
1053 | * |
||
1054 | * <code> |
||
1055 | * $qb = $em->createQueryBuilder() |
||
1056 | * ->select('u') |
||
1057 | * ->from('User', 'u') |
||
1058 | * ->where('u.id = ?'); |
||
1059 | * |
||
1060 | * // You can optionally programmatically build and/or expressions |
||
1061 | * $qb = $em->createQueryBuilder(); |
||
1062 | * |
||
1063 | * $or = $qb->expr()->orX(); |
||
1064 | * $or->add($qb->expr()->eq('u.id', 1)); |
||
1065 | * $or->add($qb->expr()->eq('u.id', 2)); |
||
1066 | * |
||
1067 | * $qb->update('User', 'u') |
||
1068 | * ->set('u.password', '?') |
||
1069 | * ->where($or); |
||
1070 | * </code> |
||
1071 | * |
||
1072 | * @param mixed $predicates The restriction predicates. |
||
1073 | * |
||
1074 | * @return self |
||
1075 | */ |
||
1076 | 37 | public function where($predicates) |
|
1084 | |||
1085 | /** |
||
1086 | * Adds one or more restrictions to the query results, forming a logical |
||
1087 | * conjunction with any previously specified restrictions. |
||
1088 | * |
||
1089 | * <code> |
||
1090 | * $qb = $em->createQueryBuilder() |
||
1091 | * ->select('u') |
||
1092 | * ->from('User', 'u') |
||
1093 | * ->where('u.username LIKE ?') |
||
1094 | * ->andWhere('u.is_active = 1'); |
||
1095 | * </code> |
||
1096 | * |
||
1097 | * @param mixed $where The query restrictions. |
||
1098 | * |
||
1099 | * @return self |
||
1100 | * |
||
1101 | * @see where() |
||
1102 | */ |
||
1103 | 17 | public function andWhere() |
|
1117 | |||
1118 | /** |
||
1119 | * Adds one or more restrictions to the query results, forming a logical |
||
1120 | * disjunction with any previously specified restrictions. |
||
1121 | * |
||
1122 | * <code> |
||
1123 | * $qb = $em->createQueryBuilder() |
||
1124 | * ->select('u') |
||
1125 | * ->from('User', 'u') |
||
1126 | * ->where('u.id = 1') |
||
1127 | * ->orWhere('u.id = 2'); |
||
1128 | * </code> |
||
1129 | * |
||
1130 | * @param mixed $where The WHERE statement. |
||
1131 | * |
||
1132 | * @return self |
||
1133 | * |
||
1134 | * @see where() |
||
1135 | */ |
||
1136 | 5 | public function orWhere() |
|
1150 | |||
1151 | /** |
||
1152 | * Specifies a grouping over the results of the query. |
||
1153 | * Replaces any previously specified groupings, if any. |
||
1154 | * |
||
1155 | * <code> |
||
1156 | * $qb = $em->createQueryBuilder() |
||
1157 | * ->select('u') |
||
1158 | * ->from('User', 'u') |
||
1159 | * ->groupBy('u.id'); |
||
1160 | * </code> |
||
1161 | * |
||
1162 | * @param string $groupBy The grouping expression. |
||
1163 | * |
||
1164 | * @return self |
||
1165 | */ |
||
1166 | 7 | public function groupBy($groupBy) |
|
1170 | |||
1171 | /** |
||
1172 | * Adds a grouping expression to the query. |
||
1173 | * |
||
1174 | * <code> |
||
1175 | * $qb = $em->createQueryBuilder() |
||
1176 | * ->select('u') |
||
1177 | * ->from('User', 'u') |
||
1178 | * ->groupBy('u.lastLogin') |
||
1179 | * ->addGroupBy('u.createdAt'); |
||
1180 | * </code> |
||
1181 | * |
||
1182 | * @param string $groupBy The grouping expression. |
||
1183 | * |
||
1184 | * @return self |
||
1185 | */ |
||
1186 | 1 | public function addGroupBy($groupBy) |
|
1190 | |||
1191 | /** |
||
1192 | * Specifies a restriction over the groups of the query. |
||
1193 | * Replaces any previous having restrictions, if any. |
||
1194 | * |
||
1195 | * @param mixed $having The restriction over the groups. |
||
1196 | * |
||
1197 | * @return self |
||
1198 | */ |
||
1199 | 3 | public function having($having) |
|
1207 | |||
1208 | /** |
||
1209 | * Adds a restriction over the groups of the query, forming a logical |
||
1210 | * conjunction with any existing having restrictions. |
||
1211 | * |
||
1212 | * @param mixed $having The restriction to append. |
||
1213 | * |
||
1214 | * @return self |
||
1215 | */ |
||
1216 | 2 | public function andHaving($having) |
|
1230 | |||
1231 | /** |
||
1232 | * Adds a restriction over the groups of the query, forming a logical |
||
1233 | * disjunction with any existing having restrictions. |
||
1234 | * |
||
1235 | * @param mixed $having The restriction to add. |
||
1236 | * |
||
1237 | * @return self |
||
1238 | */ |
||
1239 | 1 | public function orHaving($having) |
|
1253 | |||
1254 | /** |
||
1255 | * Specifies an ordering for the query results. |
||
1256 | * Replaces any previously specified orderings, if any. |
||
1257 | * |
||
1258 | * @param string|Expr\OrderBy $sort The ordering expression. |
||
1259 | * @param string $order The ordering direction. |
||
1260 | * |
||
1261 | * @return self |
||
1262 | */ |
||
1263 | 10 | public function orderBy($sort, $order = null) |
|
1269 | |||
1270 | /** |
||
1271 | * Adds an ordering to the query results. |
||
1272 | * |
||
1273 | * @param string|Expr\OrderBy $sort The ordering expression. |
||
1274 | * @param string $order The ordering direction. |
||
1275 | * |
||
1276 | * @return self |
||
1277 | */ |
||
1278 | 4 | public function addOrderBy($sort, $order = null) |
|
1284 | |||
1285 | /** |
||
1286 | * Adds criteria to the query. |
||
1287 | * |
||
1288 | * Adds where expressions with AND operator. |
||
1289 | * Adds orderings. |
||
1290 | * Overrides firstResult and maxResults if they're set. |
||
1291 | * |
||
1292 | * @param Criteria $criteria |
||
1293 | * |
||
1294 | * @return self |
||
1295 | * |
||
1296 | * @throws Query\QueryException |
||
1297 | */ |
||
1298 | 13 | public function addCriteria(Criteria $criteria) |
|
1343 | |||
1344 | /** |
||
1345 | * Gets a query part by its name. |
||
1346 | * |
||
1347 | * @param string $queryPartName |
||
1348 | * |
||
1349 | * @return mixed $queryPart |
||
1350 | * |
||
1351 | * @todo Rename: getQueryPart (or remove?) |
||
1352 | */ |
||
1353 | 92 | public function getDQLPart($queryPartName) |
|
1357 | |||
1358 | /** |
||
1359 | * Gets all query parts. |
||
1360 | * |
||
1361 | * @return array $dqlParts |
||
1362 | * |
||
1363 | * @todo Rename: getQueryParts (or remove?) |
||
1364 | */ |
||
1365 | 1 | public function getDQLParts() |
|
1369 | |||
1370 | /** |
||
1371 | * @return string |
||
1372 | */ |
||
1373 | 1 | private function _getDQLForDelete() |
|
1380 | |||
1381 | /** |
||
1382 | * @return string |
||
1383 | */ |
||
1384 | 2 | private function _getDQLForUpdate() |
|
1392 | |||
1393 | /** |
||
1394 | * @return string |
||
1395 | */ |
||
1396 | 74 | private function _getDQLForSelect() |
|
1431 | |||
1432 | /** |
||
1433 | * @param string $queryPartName |
||
1434 | * @param array $options |
||
1435 | * |
||
1436 | * @return string |
||
1437 | */ |
||
1438 | 77 | private function _getReducedDQLQueryPart($queryPartName, $options = array()) |
|
1450 | |||
1451 | /** |
||
1452 | * Resets DQL parts. |
||
1453 | * |
||
1454 | * @param array|null $parts |
||
1455 | * |
||
1456 | * @return self |
||
1457 | */ |
||
1458 | 2 | public function resetDQLParts($parts = null) |
|
1470 | |||
1471 | /** |
||
1472 | * Resets single DQL part. |
||
1473 | * |
||
1474 | * @param string $part |
||
1475 | * |
||
1476 | * @return self |
||
1477 | */ |
||
1478 | 3 | public function resetDQLPart($part) |
|
1485 | |||
1486 | /** |
||
1487 | * Gets a string representation of this QueryBuilder which corresponds to |
||
1488 | * the final DQL query being constructed. |
||
1489 | * |
||
1490 | * @return string The string representation of this QueryBuilder. |
||
1491 | */ |
||
1492 | 5 | public function __toString() |
|
1496 | |||
1497 | /** |
||
1498 | * Deep clones all expression objects in the DQL parts. |
||
1499 | * |
||
1500 | * @return void |
||
1501 | */ |
||
1502 | 3 | public function __clone() |
|
1524 | } |
||
1525 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: