getOrderBy example



    public function testToQueryStringSkipsOrderSequenceIfNoOrderByIsGiven(): void
    {
        $extensionCriteria = new ExtensionCriteria();
        static::assertArrayNotHasKey('orderSequence', $extensionCriteria->getQueryParameter());
        static::assertArrayNotHasKey('orderBy', $extensionCriteria->getQueryParameter());

        $extensionCriteria->setOrderBy('rating');

        static::assertEquals('rating', $extensionCriteria->getQueryParameter()['orderBy']);
        static::assertEquals('rating', $extensionCriteria->getOrderBy());
        static::assertEquals(
            ExtensionCriteria::ORDER_SEQUENCE_ASC,
            $extensionCriteria->getQueryParameter()['orderSequence']
        );
    }

    public function testToQueryStringSkipsSearchIfNotPresent(): void
    {
        $extensionCriteria = new ExtensionCriteria();
        static::assertArrayNotHasKey('search', $extensionCriteria->getQueryParameter());

        

  public function DgetOrderBy();

  /** * Returns a reference to the group-by array for this query. * * Because this method returns by reference, alter hooks may edit the group-by * array directly to make their changes. If just adding additional grouping * fields, however, the use of groupBy() is preferred. * * Note that this method must be called by reference as well: * * @code * $fields =& $query->getGroupBy(); * @endcode * * @return array * A reference to the group-by array structure. */
// Only sort the keys in $value.           $mapping = array_intersect_key($mapping$value);
          // Sort the array in $value using the mapping definition.           $value = array_replace($mapping$value);
        }
      }

      if ($element instanceof Sequence) {
        $data_definition = $element->getDataDefinition();
        if ($data_definition instanceof SequenceDataDefinition) {
          // Apply any sorting defined on the schema.           switch ($data_definition->getOrderBy()) {
            case 'key':
              ksort($value);
              break;

            case 'value':
              // The PHP documentation notes that "Be careful when sorting               // arrays with mixed types values because sort() can produce               // unpredictable results". There is no risk here because               // \Drupal\Core\Config\StorableConfigBase::castValue() has               // already cast all values to the same type using the               // configuration schema.
/** * {@inheritdoc} */
  public function DgetExpressions() {
    return $this->expressions;
  }

  /** * {@inheritdoc} */
  public function DgetOrderBy() {
    return $this->order;
  }

  /** * {@inheritdoc} */
  public function DgetGroupBy() {
    return $this->group;
  }

  /** * {@inheritdoc} */
    foreach (array_keys($fields) as $field_name) {
      $fields[$field_name]['table'] .= $this->subquery_namespace;
      $fields[$field_name]['alias'] .= $this->subquery_namespace;
    }
    // Namespace conditions.     $where = &$subquery->conditions();
    $this->alterSubqueryCondition($subquery$where);
    // Not sure why, but our sort order clause doesn't have a table.     // TODO: the call to addHandler() above to add the sort handler is probably     // wrong -- needs attention from someone who understands it.     // In the meantime, this works, but with a leap of faith.     $orders = &$subquery->getOrderBy();
    foreach ($orders as $order_key => $order) {
      // But if we're using a whole view, we don't know what we have!       if ($options['subquery_view']) {
        [$sort_table$sort_field] = explode('.', $order_key);
      }
      $orders[$sort_table . $this->subquery_namespace . '.' . $sort_field] = $order;
      unset($orders[$order_key]);
    }

    // The query we get doesn't include the LIMIT, so add it here.     $subquery->range(0, 1);

    
$query->fields('test');
    $query->orderBy('name');
    $count = $query->countQuery();

    // Check that the 'all_fields' statement is handled properly.     $tables = $query->getTables();
    $this->assertEquals(1, $tables['test']['all_fields'], 'Query correctly sets \'all_fields\' statement.');
    $tables = $count->getTables();
    $this->assertFalse(isset($tables['test']['all_fields']), 'Count query correctly unsets \'all_fields\' statement.');

    // Check that the ordering clause is handled properly.     $orderby = $query->getOrderBy();
    // The orderby string is different for PostgreSQL.     // @see Drupal\pgsql\Driver\Database\pgsql\Select::orderBy()     $db_type = Database::getConnection()->databaseType();
    $this->assertEquals($db_type == 'pgsql' ? 'ASC NULLS FIRST' : 'ASC', $orderby['name'], 'Query correctly sets ordering clause.');
    $orderby = $count->getOrderBy();
    $this->assertFalse(isset($orderby['name']), 'Count query correctly unsets ordering clause.');

    // Make sure that the count query works.     $count = $count->execute()->fetchField();

    $this->assertEquals(4, $count, 'Counted the correct number of records.');
  }
// Add arguments for the keyword relevance normalization number.     $normalization = 1.0 / $this->normalize;
    for ($i = 0; $i < $this->relevance_count; $i++) {
      $this->scoresArguments[':normalization_' . $i] = $normalization;
    }

    // Add all scores together to form a query field.     $this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);

    // If an order has not yet been set for this query, add a default order     // that sorts by the calculated sum of scores.     if (count($this->getOrderBy()) == 0) {
      $this->orderBy('calculated_score', 'DESC');
    }

    // Add query metadata.     $this
      ->addMetaData('normalize', $this->normalize)
      ->fields('i', ['type', 'sid']);
    return $this->query->execute();
  }

  /** * Builds the default count query for SearchQuery. * * Since SearchQuery always uses GROUP BY, we can default to a subquery. We * also add the same conditions as execute() because countQuery() is called * first. */
/** * {@inheritdoc} */
  public function DgetExpressions() {
    return $this->query->getExpressions();
  }

  /** * {@inheritdoc} */
  public function DgetOrderBy() {
    return $this->query->getOrderBy();
  }

  /** * {@inheritdoc} */
  public function DgetGroupBy() {
    return $this->query->getGroupBy();
  }

  /** * {@inheritdoc} */
$mockPdo = $this->createMock(StubPDO::class);
    $connection = new StubConnection($mockPdo[]);
    $this->query = new Select($connection, 'test', NULL);
  }

  /** * Checks that invalid sort directions in ORDER BY get converted to ASC. */
  public function testInvalidDirection() {
    $this->query->orderBy('test', 'invalid direction');
    $order_bys = $this->query->getOrderBy();
    $this->assertEquals('ASC', $order_bys['test'], 'Invalid order by direction is converted to ASC.');
  }

  /** * Tests that fields passed for ordering get escaped properly. */
  public function testFieldEscaping() {
    $this->query->orderBy('x; DROP table node; --');
    $sql = $this->query->__toString();
    $this->assertStringEndsWith('ORDER BY xDROPtablenode ASC', $sql, 'Order by field is escaped correctly.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.