orderRandom example


  public function orderBy($field$direction = 'ASC') {
    $this->query->orderBy($field$direction);
    return $this;
  }

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

  /** * {@inheritdoc} */
  public function range($start = NULL, $length = NULL) {
    $this->query->range($start$length);
    return $this;
  }

  
->orderBy('id')
      ->execute()
      ->fetchCol();
    $this->assertEquals($expected_ids$ordered_ids, 'A query without random ordering returns IDs in the correct order.');

    // Now perform the same query, but instead choose a random ordering. We     // expect this to contain a differently ordered version of the original     // result.     $randomized_ids = $this->connection->select('test', 't')
      ->fields('t', ['id'])
      ->range(0, $number_of_items)
      ->orderRandom()
      ->execute()
      ->fetchCol();
    $this->assertNotEquals($ordered_ids$randomized_ids, 'A query with random ordering returns an unordered set of IDs.');
    $sorted_ids = $randomized_ids;
    sort($sorted_ids);
    $this->assertEquals($ordered_ids$sorted_ids, 'After sorting the random list, the result matches the original query.');

    // Now perform the exact same query again, and make sure the order is     // different.     $randomized_ids_second_set = $this->connection->select('test', 't')
      ->fields('t', ['id'])
      

      if (!empty($this->having) && $condition = $this->buildCondition('having')) {
        $query->havingCondition($condition);
      }
    }

    if (!$this->getCountOptimized) {
      // we only add the orderby if we're not counting.       if ($this->orderby) {
        foreach ($this->orderby as $order) {
          if ($order['field'] == 'rand_') {
            $query->orderRandom();
          }
          else {
            $query->orderBy($order['field']$order['direction']);
          }
        }
      }
    }

    if (!empty($this->where) && $condition = $this->buildCondition('where')) {
      $query->condition($condition);
    }

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