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 = [ |
||
| 61 | 'distinct' => false, |
||
| 62 | 'select' => [], |
||
| 63 | 'from' => [], |
||
| 64 | 'join' => [], |
||
| 65 | 'set' => [], |
||
| 66 | 'where' => null, |
||
| 67 | 'groupBy' => [], |
||
| 68 | 'having' => null, |
||
| 69 | 'orderBy' => [] |
||
| 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 = []; |
||
| 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 | * The map of query hints. |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | private $_hints = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>. |
||
| 156 | * |
||
| 157 | * @param EntityManagerInterface $em The EntityManager to use. |
||
| 158 | */ |
||
| 159 | 117 | public function __construct(EntityManagerInterface $em) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
||
| 167 | * This producer method is intended for convenient inline usage. Example: |
||
| 168 | * |
||
| 169 | * <code> |
||
| 170 | * $qb = $em->createQueryBuilder(); |
||
| 171 | * $qb |
||
| 172 | * ->select('u') |
||
| 173 | * ->from('User', 'u') |
||
| 174 | * ->where($qb->expr()->eq('u.id', 1)); |
||
| 175 | * </code> |
||
| 176 | * |
||
| 177 | * For more complex expression construction, consider storing the expression |
||
| 178 | * builder object in a local variable. |
||
| 179 | * |
||
| 180 | * @return Query\Expr |
||
| 181 | */ |
||
| 182 | 11 | public function expr() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * Enable/disable second level query (result) caching for this query. |
||
| 190 | * |
||
| 191 | * @param boolean $cacheable |
||
| 192 | * |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | 1 | public function setCacheable($cacheable) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise. |
||
| 204 | */ |
||
| 205 | 1 | public function isCacheable() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $cacheRegion |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | 1 | public function setCacheRegion($cacheRegion) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Obtain the name of the second level query cache region in which query results will be stored |
||
| 224 | * |
||
| 225 | * @return string|null The cache region name; NULL indicates the default region. |
||
| 226 | */ |
||
| 227 | 1 | public function getCacheRegion() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return integer |
||
| 234 | */ |
||
| 235 | 1 | public function getLifetime() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Sets the life-time for this query into second level cache. |
||
| 242 | * |
||
| 243 | * @param integer $lifetime |
||
| 244 | * |
||
| 245 | * @return self |
||
| 246 | */ |
||
| 247 | 1 | public function setLifetime($lifetime) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return integer |
||
| 256 | */ |
||
| 257 | 1 | public function getCacheMode() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param integer $cacheMode |
||
| 264 | * |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | 1 | public function setCacheMode($cacheMode) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Sets a query hint. |
||
| 276 | * |
||
| 277 | * @param string $name The name of the hint. |
||
| 278 | * @param mixed $value The value of the hint. |
||
| 279 | * |
||
| 280 | * @return self |
||
| 281 | */ |
||
| 282 | 1 | public function setHint($name, $value) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Gets the value of a query hint. If the hint name is not recognized, FALSE is returned. |
||
| 291 | * |
||
| 292 | * @param string $name The name of the hint. |
||
| 293 | * |
||
| 294 | * @return mixed The value of the hint or FALSE, if the hint name is not recognized. |
||
| 295 | */ |
||
| 296 | 1 | public function getHint($name) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Check if the query has a hint |
||
| 303 | * |
||
| 304 | * @param string $name The name of the hint |
||
| 305 | * |
||
| 306 | * @return bool False if the query does not have any hint |
||
| 307 | */ |
||
| 308 | 1 | public function hasHint($name) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Return the key value map of query hints that are currently set. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | 1 | public function getHints() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Gets the type of the currently built query. |
||
| 325 | * |
||
| 326 | * @return integer |
||
| 327 | */ |
||
| 328 | 4 | public function getType() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Gets the associated EntityManager for this query builder. |
||
| 335 | * |
||
| 336 | * @return EntityManager |
||
| 337 | */ |
||
| 338 | 1 | public function getEntityManager() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Gets the state of this query builder instance. |
||
| 345 | * |
||
| 346 | * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
||
| 347 | */ |
||
| 348 | 2 | public function getState() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Gets the complete DQL string formed by the current specifications of this QueryBuilder. |
||
| 355 | * |
||
| 356 | * <code> |
||
| 357 | * $qb = $em->createQueryBuilder() |
||
| 358 | * ->select('u') |
||
| 359 | * ->from('User', 'u'); |
||
| 360 | * echo $qb->getDql(); // SELECT u FROM User u |
||
| 361 | * </code> |
||
| 362 | * |
||
| 363 | * @return string The DQL query string. |
||
| 364 | */ |
||
| 365 | 81 | public function getDQL() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Constructs a Query instance from the current specifications of the builder. |
||
| 394 | * |
||
| 395 | * <code> |
||
| 396 | * $qb = $em->createQueryBuilder() |
||
| 397 | * ->select('u') |
||
| 398 | * ->from('User', 'u'); |
||
| 399 | * $q = $qb->getQuery(); |
||
| 400 | * $results = $q->execute(); |
||
| 401 | * </code> |
||
| 402 | * |
||
| 403 | * @return Query |
||
| 404 | */ |
||
| 405 | 70 | public function getQuery() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Finds the root entity alias of the joined entity. |
||
| 440 | * |
||
| 441 | * @param string $alias The alias of the new join entity |
||
| 442 | * @param string $parentAlias The parent entity alias of the join relationship |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | 29 | private function findRootAlias($alias, $parentAlias) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Gets the FIRST root alias of the query. This is the first entity alias involved |
||
| 467 | * in the construction of the query. |
||
| 468 | * |
||
| 469 | * <code> |
||
| 470 | * $qb = $em->createQueryBuilder() |
||
| 471 | * ->select('u') |
||
| 472 | * ->from('User', 'u'); |
||
| 473 | * |
||
| 474 | * echo $qb->getRootAlias(); // u |
||
| 475 | * </code> |
||
| 476 | * |
||
| 477 | * @deprecated Please use $qb->getRootAliases() instead. |
||
| 478 | * @throws \RuntimeException |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 3 | public function getRootAlias() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Gets the root aliases of the query. This is the entity aliases involved |
||
| 495 | * in the construction of the query. |
||
| 496 | * |
||
| 497 | * <code> |
||
| 498 | * $qb = $em->createQueryBuilder() |
||
| 499 | * ->select('u') |
||
| 500 | * ->from('User', 'u'); |
||
| 501 | * |
||
| 502 | * $qb->getRootAliases(); // array('u') |
||
| 503 | * </code> |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | 45 | public function getRootAliases() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Gets all the aliases that have been used in the query. |
||
| 528 | * Including all select root aliases and join aliases |
||
| 529 | * |
||
| 530 | * <code> |
||
| 531 | * $qb = $em->createQueryBuilder() |
||
| 532 | * ->select('u') |
||
| 533 | * ->from('User', 'u') |
||
| 534 | * ->join('u.articles','a'); |
||
| 535 | * |
||
| 536 | * $qb->getAllAliases(); // array('u','a') |
||
| 537 | * </code> |
||
| 538 | * @return array |
||
| 539 | */ |
||
| 540 | 15 | public function getAllAliases() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Gets the root entities of the query. This is the entity aliases involved |
||
| 547 | * in the construction of the query. |
||
| 548 | * |
||
| 549 | * <code> |
||
| 550 | * $qb = $em->createQueryBuilder() |
||
| 551 | * ->select('u') |
||
| 552 | * ->from('User', 'u'); |
||
| 553 | * |
||
| 554 | * $qb->getRootEntities(); // array('User') |
||
| 555 | * </code> |
||
| 556 | * |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | 1 | public function getRootEntities() |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Sets a query parameter for the query being constructed. |
||
| 580 | * |
||
| 581 | * <code> |
||
| 582 | * $qb = $em->createQueryBuilder() |
||
| 583 | * ->select('u') |
||
| 584 | * ->from('User', 'u') |
||
| 585 | * ->where('u.id = :user_id') |
||
| 586 | * ->setParameter('user_id', 1); |
||
| 587 | * </code> |
||
| 588 | * |
||
| 589 | * @param string|integer $key The parameter position or name. |
||
| 590 | * @param mixed $value The parameter value. |
||
| 591 | * @param string|null $type PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constant |
||
| 592 | * |
||
| 593 | * @return self |
||
| 594 | */ |
||
| 595 | 8 | public function setParameter($key, $value, $type = null) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Sets a collection of query parameters for the query being constructed. |
||
| 623 | * |
||
| 624 | * <code> |
||
| 625 | * $qb = $em->createQueryBuilder() |
||
| 626 | * ->select('u') |
||
| 627 | * ->from('User', 'u') |
||
| 628 | * ->where('u.id = :user_id1 OR u.id = :user_id2') |
||
| 629 | * ->setParameters(new ArrayCollection(array( |
||
| 630 | * new Parameter('user_id1', 1), |
||
| 631 | * new Parameter('user_id2', 2) |
||
| 632 | * ))); |
||
| 633 | * </code> |
||
| 634 | * |
||
| 635 | * @param \Doctrine\Common\Collections\ArrayCollection|array $parameters The query parameters to set. |
||
| 636 | * |
||
| 637 | * @return self |
||
| 638 | */ |
||
| 639 | 4 | public function setParameters($parameters) |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Gets all defined query parameters for the query being constructed. |
||
| 661 | * |
||
| 662 | * @return \Doctrine\Common\Collections\ArrayCollection The currently defined query parameters. |
||
| 663 | */ |
||
| 664 | 2 | public function getParameters() |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Gets a (previously set) query parameter of the query being constructed. |
||
| 671 | * |
||
| 672 | * @param mixed $key The key (index or name) of the bound parameter. |
||
| 673 | * |
||
| 674 | * @return Query\Parameter|null The value of the bound parameter. |
||
| 675 | */ |
||
| 676 | 12 | public function getParameter($key) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Sets the position of the first result to retrieve (the "offset"). |
||
| 692 | * |
||
| 693 | * @param integer $firstResult The first result to return. |
||
| 694 | * |
||
| 695 | * @return self |
||
| 696 | */ |
||
| 697 | 2 | public function setFirstResult($firstResult) |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Gets the position of the first result the query object was set to retrieve (the "offset"). |
||
| 706 | * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
||
| 707 | * |
||
| 708 | * @return integer The position of the first result. |
||
| 709 | */ |
||
| 710 | 2 | public function getFirstResult() |
|
| 714 | |||
| 715 | /** |
||
| 716 | * Sets the maximum number of results to retrieve (the "limit"). |
||
| 717 | * |
||
| 718 | * @param integer $maxResults The maximum number of results to retrieve. |
||
| 719 | * |
||
| 720 | * @return self |
||
| 721 | */ |
||
| 722 | 3 | public function setMaxResults($maxResults) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
||
| 731 | * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
||
| 732 | * |
||
| 733 | * @return integer Maximum number of results. |
||
| 734 | */ |
||
| 735 | 2 | public function getMaxResults() |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Either appends to or replaces a single, generic query part. |
||
| 742 | * |
||
| 743 | * The available parts are: 'select', 'from', 'join', 'set', 'where', |
||
| 744 | * 'groupBy', 'having' and 'orderBy'. |
||
| 745 | * |
||
| 746 | * @param string $dqlPartName The DQL part name. |
||
| 747 | * @param object|array $dqlPart An Expr object. |
||
| 748 | * @param bool $append Whether to append (true) or replace (false). |
||
| 749 | * |
||
| 750 | * @return self |
||
| 751 | */ |
||
| 752 | 113 | public function add($dqlPartName, $dqlPart, $append = false) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Specifies an item that is to be returned in the query result. |
||
| 802 | * Replaces any previously specified selections, if any. |
||
| 803 | * |
||
| 804 | * <code> |
||
| 805 | * $qb = $em->createQueryBuilder() |
||
| 806 | * ->select('u', 'p') |
||
| 807 | * ->from('User', 'u') |
||
| 808 | * ->leftJoin('u.Phonenumbers', 'p'); |
||
| 809 | * </code> |
||
| 810 | * |
||
| 811 | * @param mixed $select The selection expressions. |
||
| 812 | * |
||
| 813 | * @return self |
||
| 814 | */ |
||
| 815 | 106 | public function select($select = null) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Adds a DISTINCT flag to this query. |
||
| 830 | * |
||
| 831 | * <code> |
||
| 832 | * $qb = $em->createQueryBuilder() |
||
| 833 | * ->select('u') |
||
| 834 | * ->distinct() |
||
| 835 | * ->from('User', 'u'); |
||
| 836 | * </code> |
||
| 837 | * |
||
| 838 | * @param bool $flag |
||
| 839 | * |
||
| 840 | * @return self |
||
| 841 | */ |
||
| 842 | 1 | public function distinct($flag = true) |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Adds an item that is to be returned in the query result. |
||
| 851 | * |
||
| 852 | * <code> |
||
| 853 | * $qb = $em->createQueryBuilder() |
||
| 854 | * ->select('u') |
||
| 855 | * ->addSelect('p') |
||
| 856 | * ->from('User', 'u') |
||
| 857 | * ->leftJoin('u.Phonenumbers', 'p'); |
||
| 858 | * </code> |
||
| 859 | * |
||
| 860 | * @param mixed $select The selection expression. |
||
| 861 | * |
||
| 862 | * @return self |
||
| 863 | */ |
||
| 864 | 1 | public function addSelect($select = null) |
|
| 876 | |||
| 877 | /** |
||
| 878 | * Turns the query being built into a bulk delete query that ranges over |
||
| 879 | * a certain entity type. |
||
| 880 | * |
||
| 881 | * <code> |
||
| 882 | * $qb = $em->createQueryBuilder() |
||
| 883 | * ->delete('User', 'u') |
||
| 884 | * ->where('u.id = :user_id') |
||
| 885 | * ->setParameter('user_id', 1); |
||
| 886 | * </code> |
||
| 887 | * |
||
| 888 | * @param string $delete The class/type whose instances are subject to the deletion. |
||
| 889 | * @param string $alias The class/type alias used in the constructed query. |
||
| 890 | * |
||
| 891 | * @return self |
||
| 892 | */ |
||
| 893 | 4 | public function delete($delete = null, $alias = null) |
|
| 903 | |||
| 904 | /** |
||
| 905 | * Turns the query being built into a bulk update query that ranges over |
||
| 906 | * a certain entity type. |
||
| 907 | * |
||
| 908 | * <code> |
||
| 909 | * $qb = $em->createQueryBuilder() |
||
| 910 | * ->update('User', 'u') |
||
| 911 | * ->set('u.password', '?1') |
||
| 912 | * ->where('u.id = ?2'); |
||
| 913 | * </code> |
||
| 914 | * |
||
| 915 | * @param string $update The class/type whose instances are subject to the update. |
||
| 916 | * @param string $alias The class/type alias used in the constructed query. |
||
| 917 | * |
||
| 918 | * @return self |
||
| 919 | */ |
||
| 920 | 3 | public function update($update = null, $alias = null) |
|
| 930 | |||
| 931 | /** |
||
| 932 | * Creates and adds a query root corresponding to the entity identified by the given alias, |
||
| 933 | * forming a cartesian product with any existing query roots. |
||
| 934 | * |
||
| 935 | * <code> |
||
| 936 | * $qb = $em->createQueryBuilder() |
||
| 937 | * ->select('u') |
||
| 938 | * ->from('User', 'u'); |
||
| 939 | * </code> |
||
| 940 | * |
||
| 941 | * @param string $from The class name. |
||
| 942 | * @param string $alias The alias of the class. |
||
| 943 | * @param string $indexBy The index for the from. |
||
| 944 | * |
||
| 945 | * @return self |
||
| 946 | */ |
||
| 947 | 105 | public function from($from, $alias, $indexBy = null) |
|
| 951 | |||
| 952 | /** |
||
| 953 | * Updates a query root corresponding to an entity setting its index by. This method is intended to be used with |
||
| 954 | * EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it |
||
| 955 | * setting an index by. |
||
| 956 | * |
||
| 957 | * <code> |
||
| 958 | * $qb = $userRepository->createQueryBuilder('u') |
||
| 959 | * ->indexBy('u', 'u.id'); |
||
| 960 | * |
||
| 961 | * // Is equivalent to... |
||
| 962 | * |
||
| 963 | * $qb = $em->createQueryBuilder() |
||
| 964 | * ->select('u') |
||
| 965 | * ->from('User', 'u', 'u.id'); |
||
| 966 | * </code> |
||
| 967 | * |
||
| 968 | * @param string $alias The root alias of the class. |
||
| 969 | * @param string $indexBy The index for the from. |
||
| 970 | * |
||
| 971 | * @return self |
||
| 972 | * |
||
| 973 | * @throws Query\QueryException |
||
| 974 | */ |
||
| 975 | 2 | public function indexBy($alias, $indexBy) |
|
| 996 | |||
| 997 | /** |
||
| 998 | * Creates and adds a join over an entity association to the query. |
||
| 999 | * |
||
| 1000 | * The entities in the joined association will be fetched as part of the query |
||
| 1001 | * result if the alias used for the joined association is placed in the select |
||
| 1002 | * expressions. |
||
| 1003 | * |
||
| 1004 | * <code> |
||
| 1005 | * $qb = $em->createQueryBuilder() |
||
| 1006 | * ->select('u') |
||
| 1007 | * ->from('User', 'u') |
||
| 1008 | * ->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
| 1009 | * </code> |
||
| 1010 | * |
||
| 1011 | * @param string $join The relationship to join. |
||
| 1012 | * @param string $alias The alias of the join. |
||
| 1013 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
| 1014 | * @param string|null $condition The condition for the join. |
||
| 1015 | * @param string|null $indexBy The index for the join. |
||
| 1016 | * |
||
| 1017 | * @return self |
||
| 1018 | */ |
||
| 1019 | 8 | public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Creates and adds a join over an entity association to the query. |
||
| 1026 | * |
||
| 1027 | * The entities in the joined association will be fetched as part of the query |
||
| 1028 | * result if the alias used for the joined association is placed in the select |
||
| 1029 | * expressions. |
||
| 1030 | * |
||
| 1031 | * [php] |
||
| 1032 | * $qb = $em->createQueryBuilder() |
||
| 1033 | * ->select('u') |
||
| 1034 | * ->from('User', 'u') |
||
| 1035 | * ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
| 1036 | * |
||
| 1037 | * @param string $join The relationship to join. |
||
| 1038 | * @param string $alias The alias of the join. |
||
| 1039 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
| 1040 | * @param string|null $condition The condition for the join. |
||
| 1041 | * @param string|null $indexBy The index for the join. |
||
| 1042 | * |
||
| 1043 | * @return self |
||
| 1044 | */ |
||
| 1045 | 15 | public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Creates and adds a left join over an entity association to the query. |
||
| 1060 | * |
||
| 1061 | * The entities in the joined association will be fetched as part of the query |
||
| 1062 | * result if the alias used for the joined association is placed in the select |
||
| 1063 | * expressions. |
||
| 1064 | * |
||
| 1065 | * <code> |
||
| 1066 | * $qb = $em->createQueryBuilder() |
||
| 1067 | * ->select('u') |
||
| 1068 | * ->from('User', 'u') |
||
| 1069 | * ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); |
||
| 1070 | * </code> |
||
| 1071 | * |
||
| 1072 | * @param string $join The relationship to join. |
||
| 1073 | * @param string $alias The alias of the join. |
||
| 1074 | * @param string|null $conditionType The condition type constant. Either ON or WITH. |
||
| 1075 | * @param string|null $condition The condition for the join. |
||
| 1076 | * @param string|null $indexBy The index for the join. |
||
| 1077 | * |
||
| 1078 | * @return self |
||
| 1079 | */ |
||
| 1080 | 15 | public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null) |
|
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Sets a new value for a field in a bulk update query. |
||
| 1095 | * |
||
| 1096 | * <code> |
||
| 1097 | * $qb = $em->createQueryBuilder() |
||
| 1098 | * ->update('User', 'u') |
||
| 1099 | * ->set('u.password', '?1') |
||
| 1100 | * ->where('u.id = ?2'); |
||
| 1101 | * </code> |
||
| 1102 | * |
||
| 1103 | * @param string $key The key/field to set. |
||
| 1104 | * @param string $value The value, expression, placeholder, etc. |
||
| 1105 | * |
||
| 1106 | * @return self |
||
| 1107 | */ |
||
| 1108 | 2 | public function set($key, $value) |
|
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Specifies one or more restrictions to the query result. |
||
| 1115 | * Replaces any previously specified restrictions, if any. |
||
| 1116 | * |
||
| 1117 | * <code> |
||
| 1118 | * $qb = $em->createQueryBuilder() |
||
| 1119 | * ->select('u') |
||
| 1120 | * ->from('User', 'u') |
||
| 1121 | * ->where('u.id = ?'); |
||
| 1122 | * |
||
| 1123 | * // You can optionally programmatically build and/or expressions |
||
| 1124 | * $qb = $em->createQueryBuilder(); |
||
| 1125 | * |
||
| 1126 | * $or = $qb->expr()->orX(); |
||
| 1127 | * $or->add($qb->expr()->eq('u.id', 1)); |
||
| 1128 | * $or->add($qb->expr()->eq('u.id', 2)); |
||
| 1129 | * |
||
| 1130 | * $qb->update('User', 'u') |
||
| 1131 | * ->set('u.password', '?') |
||
| 1132 | * ->where($or); |
||
| 1133 | * </code> |
||
| 1134 | * |
||
| 1135 | * @param mixed $predicates The restriction predicates. |
||
| 1136 | * |
||
| 1137 | * @return self |
||
| 1138 | */ |
||
| 1139 | 40 | public function where($predicates) |
|
| 1147 | |||
| 1148 | /** |
||
| 1149 | * Adds one or more restrictions to the query results, forming a logical |
||
| 1150 | * conjunction with any previously specified restrictions. |
||
| 1151 | * |
||
| 1152 | * <code> |
||
| 1153 | * $qb = $em->createQueryBuilder() |
||
| 1154 | * ->select('u') |
||
| 1155 | * ->from('User', 'u') |
||
| 1156 | * ->where('u.username LIKE ?') |
||
| 1157 | * ->andWhere('u.is_active = 1'); |
||
| 1158 | * </code> |
||
| 1159 | * |
||
| 1160 | * @param mixed $where The query restrictions. |
||
| 1161 | * |
||
| 1162 | * @return self |
||
| 1163 | * |
||
| 1164 | * @see where() |
||
| 1165 | */ |
||
| 1166 | 17 | public function andWhere() |
|
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Adds one or more restrictions to the query results, forming a logical |
||
| 1183 | * disjunction with any previously specified restrictions. |
||
| 1184 | * |
||
| 1185 | * <code> |
||
| 1186 | * $qb = $em->createQueryBuilder() |
||
| 1187 | * ->select('u') |
||
| 1188 | * ->from('User', 'u') |
||
| 1189 | * ->where('u.id = 1') |
||
| 1190 | * ->orWhere('u.id = 2'); |
||
| 1191 | * </code> |
||
| 1192 | * |
||
| 1193 | * @param mixed $where The WHERE statement. |
||
| 1194 | * |
||
| 1195 | * @return self |
||
| 1196 | * |
||
| 1197 | * @see where() |
||
| 1198 | */ |
||
| 1199 | 5 | public function orWhere() |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Specifies a grouping over the results of the query. |
||
| 1216 | * Replaces any previously specified groupings, if any. |
||
| 1217 | * |
||
| 1218 | * <code> |
||
| 1219 | * $qb = $em->createQueryBuilder() |
||
| 1220 | * ->select('u') |
||
| 1221 | * ->from('User', 'u') |
||
| 1222 | * ->groupBy('u.id'); |
||
| 1223 | * </code> |
||
| 1224 | * |
||
| 1225 | * @param string $groupBy The grouping expression. |
||
| 1226 | * |
||
| 1227 | * @return self |
||
| 1228 | */ |
||
| 1229 | 7 | public function groupBy($groupBy) |
|
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Adds a grouping expression to the query. |
||
| 1236 | * |
||
| 1237 | * <code> |
||
| 1238 | * $qb = $em->createQueryBuilder() |
||
| 1239 | * ->select('u') |
||
| 1240 | * ->from('User', 'u') |
||
| 1241 | * ->groupBy('u.lastLogin') |
||
| 1242 | * ->addGroupBy('u.createdAt'); |
||
| 1243 | * </code> |
||
| 1244 | * |
||
| 1245 | * @param string $groupBy The grouping expression. |
||
| 1246 | * |
||
| 1247 | * @return self |
||
| 1248 | */ |
||
| 1249 | 1 | public function addGroupBy($groupBy) |
|
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Specifies a restriction over the groups of the query. |
||
| 1256 | * Replaces any previous having restrictions, if any. |
||
| 1257 | * |
||
| 1258 | * @param mixed $having The restriction over the groups. |
||
| 1259 | * |
||
| 1260 | * @return self |
||
| 1261 | */ |
||
| 1262 | 3 | public function having($having) |
|
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Adds a restriction over the groups of the query, forming a logical |
||
| 1273 | * conjunction with any existing having restrictions. |
||
| 1274 | * |
||
| 1275 | * @param mixed $having The restriction to append. |
||
| 1276 | * |
||
| 1277 | * @return self |
||
| 1278 | */ |
||
| 1279 | 2 | public function andHaving($having) |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Adds a restriction over the groups of the query, forming a logical |
||
| 1296 | * disjunction with any existing having restrictions. |
||
| 1297 | * |
||
| 1298 | * @param mixed $having The restriction to add. |
||
| 1299 | * |
||
| 1300 | * @return self |
||
| 1301 | */ |
||
| 1302 | 1 | public function orHaving($having) |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Specifies an ordering for the query results. |
||
| 1319 | * Replaces any previously specified orderings, if any. |
||
| 1320 | * |
||
| 1321 | * @param string|Expr\OrderBy $sort The ordering expression. |
||
| 1322 | * @param string $order The ordering direction. |
||
| 1323 | * |
||
| 1324 | * @return self |
||
| 1325 | */ |
||
| 1326 | 10 | public function orderBy($sort, $order = null) |
|
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Adds an ordering to the query results. |
||
| 1335 | * |
||
| 1336 | * @param string|Expr\OrderBy $sort The ordering expression. |
||
| 1337 | * @param string $order The ordering direction. |
||
| 1338 | * |
||
| 1339 | * @return self |
||
| 1340 | */ |
||
| 1341 | 4 | public function addOrderBy($sort, $order = null) |
|
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Adds criteria to the query. |
||
| 1350 | * |
||
| 1351 | * Adds where expressions with AND operator. |
||
| 1352 | * Adds orderings. |
||
| 1353 | * Overrides firstResult and maxResults if they're set. |
||
| 1354 | * |
||
| 1355 | * @param Criteria $criteria |
||
| 1356 | * |
||
| 1357 | * @return self |
||
| 1358 | * |
||
| 1359 | * @throws Query\QueryException |
||
| 1360 | */ |
||
| 1361 | 13 | public function addCriteria(Criteria $criteria) |
|
| 1406 | |||
| 1407 | /** |
||
| 1408 | * Gets a query part by its name. |
||
| 1409 | * |
||
| 1410 | * @param string $queryPartName |
||
| 1411 | * |
||
| 1412 | * @return mixed $queryPart |
||
| 1413 | * |
||
| 1414 | * @todo Rename: getQueryPart (or remove?) |
||
| 1415 | */ |
||
| 1416 | 96 | public function getDQLPart($queryPartName) |
|
| 1420 | |||
| 1421 | /** |
||
| 1422 | * Gets all query parts. |
||
| 1423 | * |
||
| 1424 | * @return array $dqlParts |
||
| 1425 | * |
||
| 1426 | * @todo Rename: getQueryParts (or remove?) |
||
| 1427 | */ |
||
| 1428 | 1 | public function getDQLParts() |
|
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @return string |
||
| 1435 | */ |
||
| 1436 | 1 | private function _getDQLForDelete() |
|
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @return string |
||
| 1446 | */ |
||
| 1447 | 2 | private function _getDQLForUpdate() |
|
| 1455 | |||
| 1456 | /** |
||
| 1457 | * @return string |
||
| 1458 | */ |
||
| 1459 | 78 | private function _getDQLForSelect() |
|
| 1494 | |||
| 1495 | /** |
||
| 1496 | * @param string $queryPartName |
||
| 1497 | * @param array $options |
||
| 1498 | * |
||
| 1499 | * @return string |
||
| 1500 | */ |
||
| 1501 | 81 | private function _getReducedDQLQueryPart($queryPartName, $options = []) |
|
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Resets DQL parts. |
||
| 1516 | * |
||
| 1517 | * @param array|null $parts |
||
| 1518 | * |
||
| 1519 | * @return self |
||
| 1520 | */ |
||
| 1521 | 2 | public function resetDQLParts($parts = null) |
|
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Resets single DQL part. |
||
| 1536 | * |
||
| 1537 | * @param string $part |
||
| 1538 | * |
||
| 1539 | * @return self |
||
| 1540 | */ |
||
| 1541 | 3 | public function resetDQLPart($part) |
|
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Gets a string representation of this QueryBuilder which corresponds to |
||
| 1551 | * the final DQL query being constructed. |
||
| 1552 | * |
||
| 1553 | * @return string The string representation of this QueryBuilder. |
||
| 1554 | */ |
||
| 1555 | 5 | public function __toString() |
|
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Deep clones all expression objects in the DQL parts. |
||
| 1562 | * |
||
| 1563 | * @return void |
||
| 1564 | */ |
||
| 1565 | 3 | public function __clone() |
|
| 1587 | } |
||
| 1588 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: