addMetaData example


  public function testMetaData() {
    $query = $this->connection->select('test');
    $query->addField('test', 'name');
    $query->addField('test', 'age', 'age');

    $data = [
      'a' => 'A',
      'b' => 'B',
    ];

    $query->addMetaData('test', $data);

    $return = $query->getMetaData('test');
    $this->assertEquals($data$return, 'Correct metadata returned.');

    $return = $query->getMetaData('nothere');
    $this->assertNull($return, 'Non-existent key returned NULL.');
  }

}
public function loadThread(EntityInterface $entity$field_name$mode$comments_per_page = 0, $pager_id = 0) {
    $data_table = $this->getDataTable();
    $query = $this->database->select($data_table, 'c');
    $query->addField('c', 'cid');
    $query
      ->condition('c.entity_id', $entity->id())
      ->condition('c.entity_type', $entity->getEntityTypeId())
      ->condition('c.field_name', $field_name)
      ->condition('c.default_langcode', 1)
      ->addTag('entity_access')
      ->addTag('comment_filter')
      ->addMetaData('base_table', 'comment')
      ->addMetaData('entity', $entity)
      ->addMetaData('field_name', $field_name);

    if ($comments_per_page) {
      $query = $query->extend(PagerSelectExtender::class)
        ->limit($comments_per_page);
      if ($pager_id) {
        $query->element($pager_id);
      }

      $count_query = $this->database->select($data_table, 'c');
      


    $query = Database::getConnection('replica')->select('test_task', 'tt');
    $query->addExpression('[tt].[pid] + 1', 'abc');
    $query->condition('priority', 1, '>');
    $query->condition('priority', 100, '<');

    $subquery = $this->connection->select('test', 'tp');
    $subquery->join('test_one_blob', 'tpb', '[tp].[id] = [tpb].[id]');
    $subquery->join('node', 'n', '[tp].[id] = [n].[nid]');
    $subquery->addTag('node_access');
    $subquery->addMetaData('account', $account);
    $subquery->addField('tp', 'id');
    $subquery->condition('age', 5, '>');
    $subquery->condition('age', 500, '<');

    $query->leftJoin($subquery, 'sq', '[tt].[pid] = [sq].[id]');
    $query->join('test_one_blob', 'tb3', '[tt].[pid] = [tb3].[id]');

    // Construct the query string.     // This is the same sequence that SelectQuery::execute() goes through.     $query->preExecute();
    $query->getArguments();
    

class NewTopicsBlock extends ForumBlockBase {

  /** * {@inheritdoc} */
  protected function buildForumQuery() {
    return Database::getConnection()->select('forum_index', 'f')
      ->fields('f')
      ->addTag('node_access')
      ->addMetaData('base_table', 'forum_index')
      ->orderBy('f.created', 'DESC')
      ->range(0, $this->configuration['block_count']);
  }

}
/** * Adds tags and metadata to the query. * * @return \Drupal\Core\Database\Query\SelectInterface * The query with additional tags and metadata. */
  protected function prepareQuery() {
    $this->query = clone $this->query();
    $this->query->addTag('migrate');
    $this->query->addTag('migrate_' . $this->migration->id());
    $this->query->addMetaData('migration', $this->migration);

    return $this->query;
  }

  /** * {@inheritdoc} */
  protected function initializeIterator() {
    // Initialize the batch size.     if ($this->batchSize == 0 && isset($this->configuration['batch_size'])) {
      // Valid batch sizes are integers >= 0.
/** * {@inheritdoc} */
  public function loadMultiple($nids$access = TRUE) {
    $query = $this->connection->select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
    $query->fields('b');
    $query->condition('b.nid', $nids, 'IN');

    if ($access) {
      $query->addTag('node_access');
      $query->addMetaData('base_table', 'book');
    }

    return $query->execute();
  }

  /** * {@inheritdoc} */
  public function getChildRelativeDepth($book_link$max_depth) {
    $query = $this->connection->select('book');
    $query->addField('book', 'depth');
    
/** * {@inheritdoc} */
  public function hasAnyTag() {
    return call_user_func_array([$this->query, 'hasAnyTag']func_get_args());
  }

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

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

  /** * {@inheritdoc} */
// 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. */
  
$entity_base_table = $entity_type->getRevisionTable();
        }
        else {
          $data_table = $entity_type->getDataTable();
          $entity_base_table = $entity_type->getBaseTable();

          if ($field_storage && $field_storage->isRevisionable() && in_array($field_storage->getName()$entity_type->getRevisionMetadataKeys())) {
            $revision_table = $entity_type->getRevisionTable();
          }
        }
        if ($data_table) {
          $this->sqlQuery->addMetaData('simple_query', FALSE);
          $entity_tables[$data_table] = $this->getTableMapping($data_table$entity_type_id);
        }
        if ($revision_table) {
          $entity_tables[$revision_table] = $this->getTableMapping($revision_table$entity_type_id);
        }
        $entity_tables[$entity_base_table] = $this->getTableMapping($entity_base_table$entity_type_id);
        $sql_column = $specifier;

        // If there are more specifiers, get the right sql column name if the         // next one is a column of this field.         if ($key < $count) {
          
throw new QueryException("No base table for " . $this->entityTypeId . ", invalid query.");
      }
    }
    $simple_query = TRUE;
    if ($this->entityType->getDataTable()) {
      $simple_query = FALSE;
    }
    $this->sqlQuery = $this->connection->select($base_table, 'base_table', ['conjunction' => $this->conjunction]);
    // Reset the tables structure, as it might have been built for a previous     // execution of this query.     $this->tables = NULL;
    $this->sqlQuery->addMetaData('entity_type', $this->entityTypeId);
    $id_field = $this->entityType->getKey('id');
    // Add the key field for fetchAllKeyed().     if (!$revision_field = $this->entityType->getKey('revision')) {
      // When there is no revision support, the key field is the entity key.       $this->sqlFields["base_table.$id_field"] = ['base_table', $id_field];
      // Now add the value column for fetchAllKeyed(). This is always the       // entity id.       $this->sqlFields["base_table.$id_field" . '_1'] = ['base_table', $id_field];
    }
    else {
      // When there is revision support, the key field is the revision key.


    // Apply any pagination options to the query.     if (isset($params[OffsetPage::KEY_NAME])) {
      $pagination = $params[OffsetPage::KEY_NAME];
    }
    else {
      $pagination = new OffsetPage(OffsetPage::DEFAULT_OFFSET, OffsetPage::SIZE_MAX);
    }
    // Add one extra element to the page to see if there are more pages needed.     $query->range($pagination->getOffset()$pagination->getSize() + 1);
    $query->addMetaData('pager_size', (int) $pagination->getSize());

    // Limit this query to the bundle type for this resource.     $bundle = $resource_type->getBundle();
    if ($bundle && ($bundle_key = $entity_type->getKey('bundle'))) {
      $query->condition(
        $bundle_key$bundle
      );
    }

    return $query;
  }

  
$query->comment($this->options['query_comment']);
    }

    // Add the query tags.     if (!empty($this->options['query_tags'])) {
      foreach ($this->options['query_tags'] as $tag) {
        $query->addTag($tag);
      }
    }

    // Add all query substitutions as metadata.     $query->addMetaData('views_substitutions', \Drupal::moduleHandler()->invokeAll('views_query_substitutions', [$this->view]));

    return $query;
  }

  /** * Get the arguments attached to the WHERE and HAVING clauses of this query. */
  public function getWhereArgs() {
    return array_merge(...array_column($this->where, 'args'), ...array_column($this->having, 'args'));
  }

  

  public function buildContent(UserInterface $user = NULL) {
    if ($user) {
      $query = $this->database->select('tracker_user', 't')
        ->extend(PagerSelectExtender::class)
        ->addMetaData('base_table', 'tracker_user')
        ->condition('t.uid', $user->id());
    }
    else {
      $query = $this->databaseReplica->select('tracker_node', 't')
        ->extend(PagerSelectExtender::class)
        ->addMetaData('base_table', 'tracker_node');
    }

    // This array acts as a placeholder for the data selected later     // while keeping the correct order.     $tracker_data = $query
      
$header[$i]['sort'] = $order['sort'];
      }
    }

    $query = $this->connection->select('forum_index', 'f')
      ->extend(PagerSelectExtender::class)
      ->extend(TableSortExtender::class);
    $query->fields('f');
    $query
      ->condition('f.tid', $tid)
      ->addTag('node_access')
      ->addMetaData('base_table', 'forum_index')
      ->orderBy('f.sticky', 'DESC')
      ->orderByHeader($header)
      ->limit($forum_per_page);

    $count_query = $this->connection->select('forum_index', 'f');
    $count_query->condition('f.tid', $tid);
    $count_query->addExpression('COUNT(*)');
    $count_query->addTag('node_access');
    $count_query->addMetaData('base_table', 'forum_index');

    $query->setCountQuery($count_query);
    
/** * Tests adding a tag and metadata to the Entity query object. * * The tags and metadata should propagate to the SQL query object. */
  public function testMetaData() {
    field_test_memorize();

    $query = $this->storage->getQuery()->accessCheck(FALSE);
    $query
      ->addTag('efq_metadata_test')
      ->addMetaData('foo', 'bar')
      ->execute();

    $mem = field_test_memorize();
    $this->assertEquals('bar', $mem['field_test_query_efq_metadata_test_alter'][0], 'Tag and metadata propagated to the SQL query object.');
  }

  /** * Tests case sensitive and in-sensitive query conditions. */
  public function testCaseSensitivity() {
    $bundle = $this->randomMachineName();

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