Author: Teoh Han Hui (teohhanhui@gmail.com)
Author: Vincent Chalamon (vincentchalamon@gmail.com)
 public function testGetOrderByPartsWithReflection()
 {
     $methodExist = $this->getFunctionMock('ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Util', 'method_exists');
     $methodExist->expects($this->any())->with(OrderBy::class, 'getParts')->willReturn('false');
     $orderBy = new OrderBy('name', 'desc');
     $this->assertEquals(['name desc'], QueryJoinParser::getOrderByParts($orderBy));
 }
Exemple #2
0
 /**
  * Determines whether the query builder has ORDER BY on entity joined through
  * to-many association.
  *
  * @param QueryBuilder    $queryBuilder
  * @param ManagerRegistry $managerRegistry
  *
  * @return bool
  */
 public static function hasOrderByOnToManyJoin(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry) : bool
 {
     if (empty($orderByParts = $queryBuilder->getDQLPart('orderBy')) || empty($joinParts = $queryBuilder->getDQLPart('join'))) {
         return false;
     }
     $orderByAliases = [];
     foreach ($orderByParts as $orderBy) {
         $parts = QueryJoinParser::getOrderByParts($orderBy);
         foreach ($parts as $part) {
             if (false !== ($pos = strpos($part, '.'))) {
                 $alias = substr($part, 0, $pos);
                 $orderByAliases[$alias] = true;
             }
         }
     }
     if (!empty($orderByAliases)) {
         foreach ($joinParts as $joins) {
             foreach ($joins as $join) {
                 $alias = QueryJoinParser::getJoinAlias($join);
                 if (isset($orderByAliases[$alias])) {
                     $relationship = QueryJoinParser::getJoinRelationship($join);
                     list($parentAlias, $association) = explode('.', $relationship);
                     $parentMetadata = QueryJoinParser::getClassMetadataFromJoinAlias($parentAlias, $queryBuilder, $managerRegistry);
                     if ($parentMetadata->isCollectionValuedAssociation($association)) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }