orConditionGroup example


  protected function buildGroup(QueryInterface $query, EntityConditionGroup $condition_group) {
    // Create a condition group using the original query.     switch ($condition_group->conjunction()) {
      case 'AND':
        $group = $query->andConditionGroup();
        break;

      case 'OR':
        $group = $query->orConditionGroup();
        break;
    }

    // Get all children of the group.     $members = $condition_group->members();

    foreach ($members as $member) {
      // If the child is simply a condition, add it to the new group.       if ($member instanceof EntityCondition) {
        if ($member->operator() == 'IS NULL') {
          $group->notExists($member->field());
        }
// Get any entity translation revision data.     if ($this->getDatabase()->schema()
      ->tableExists('entity_translation_revision')) {
      $query->leftJoin('entity_translation_revision', 'etr', '[nr].[nid] = [etr].[entity_id] AND [nr].[vid] = [etr].[revision_id]');
      $query->fields('etr', [
        'entity_type',
        'entity_id',
        'revision_id',
        'source',
        'translate',
      ]);
      $conditions = $query->orConditionGroup();
      $conditions->condition('etr.entity_type', 'node');
      $conditions->isNull('etr.entity_type');
      $query->condition($conditions);
      $query->addExpression("COALESCE([etr].[language], [n].[language])", 'language');
      $query->addField('etr', 'uid', 'etr_uid');
      $query->addField('etr', 'status', 'etr_status');
      $query->addField('etr', 'created', 'etr_created');
      $query->addField('etr', 'changed', 'etr_changed');

      $query->orderBy('etr.revision_id');
      $query->orderBy('etr.language');
    }
/** * {@inheritdoc} */
  protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = parent::buildEntityQuery($match$match_operator);
    // Allow referencing :     // - files with status "permanent"     // - or files uploaded by the current user (since newly uploaded files only     // become "permanent" after the containing entity gets validated and     // saved.)     $query->condition($query->orConditionGroup()
      ->condition('status', FileInterface::STATUS_PERMANENT)
      ->condition('uid', $this->currentUser->id()));
    return $query;
  }

  /** * {@inheritdoc} */
  public function createNewEntity($entity_type_id$bundle$label$uid) {
    $file = parent::createNewEntity($entity_type_id$bundle$label$uid);

    

  public function prepare() {
    // Enable all modules in the source except test and example modules, but     // include simpletest.     /** @var \Drupal\Core\Database\Query\SelectInterface $update */
    $update = $this->sourceDatabase->update('system')
      ->fields(['status' => 1])
      ->condition('type', 'module');
    $and = $update->andConditionGroup()
      ->condition('name', '%test%', 'NOT LIKE')
      ->condition('name', '%example%', 'NOT LIKE');
    $conditions = $update->orConditionGroup();
    $conditions->condition($and);
    $conditions->condition('name', 'simpletest');
    $update->condition($conditions);
    $update->execute();

    // Create entries for D8 test modules.     $insert = $this->sourceDatabase->insert('system')
      ->fields([
        'filename' => 'migrate_status_active_test',
        'name' => 'migrate_status_active_test',
        'type' => 'module',
        
      $this->assertEquals($expected_query->execute()$query->execute());
    }
  }

  /** * Simply provides test data to keep the actual test method tidy. */
  protected function queryConditionData() {
    // ((RED or CIRCLE) or (YELLOW and SQUARE))     $query = $this->nodeStorage->getQuery()->accessCheck(FALSE);

    $or_group = $query->orConditionGroup();

    $nested_or_group = $query->orConditionGroup();
    $nested_or_group->condition('colors', 'red', 'CONTAINS');
    $nested_or_group->condition('shapes', 'circle', 'CONTAINS');
    $or_group->condition($nested_or_group);

    $nested_and_group = $query->andConditionGroup();
    $nested_and_group->condition('colors', 'yellow', 'CONTAINS');
    $nested_and_group->condition('shapes', 'square', 'CONTAINS');
    $nested_and_group->notExists('photo.alt');
    $or_group->condition($nested_and_group);

    
public function updateIndex() {
    // Interpret the cron limit setting as the maximum number of nodes to index     // per cron run.     $limit = (int) $this->searchSettings->get('index.cron_limit');

    $query = $this->databaseReplica->select('node', 'n');
    $query->addField('n', 'nid');
    $query->leftJoin('search_dataset', 'sd', '[sd].[sid] = [n].[nid] AND [sd].[type] = :type', [':type' => $this->getPluginId()]);
    $query->addExpression('CASE MAX([sd].[reindex]) WHEN NULL THEN 0 ELSE 1 END', 'ex');
    $query->addExpression('MAX([sd].[reindex])', 'ex2');
    $query->condition(
        $query->orConditionGroup()
          ->where('[sd].[sid] IS NULL')
          ->condition('sd.reindex', 0, '<>')
      );
    $query->orderBy('ex', 'DESC')
      ->orderBy('ex2')
      ->orderBy('n.nid')
      ->groupBy('n.nid')
      ->range(0, $limit);

    $nids = $query->execute()->fetchCol();
    if (!$nids) {
      
// Filter with an OR condition group on different fields but matching on the     // same entity.     $this->queryResults = $this->entityStorage->getQuery('OR')
      ->condition('id', 1)
      ->condition('number', 31)
      ->execute();
    $this->assertResults(['1']);

    // NO simple conditions, YES complex conditions, 'AND'.     $query = $this->entityStorage->getQuery('AND');
    $and_condition_1 = $query->orConditionGroup()
      ->condition('id', '2')
      ->condition('label', $this->entities[0]->label);
    $and_condition_2 = $query->orConditionGroup()
      ->condition('id', 1)
      ->condition('label', $this->entities[3]->label);
    $this->queryResults = $query
      ->condition($and_condition_1)
      ->condition($and_condition_2)
      ->execute();
    $this->assertResults(['1']);

    
// Run the query to find matching users.     $query = $this->database
      ->select('users_field_data', 'users')
      ->extend(PagerSelectExtender::class);
    $query->fields('users', ['uid']);
    $query->condition('default_langcode', 1);
    if ($this->currentUser->hasPermission('administer users')) {
      // Administrators can also search in the otherwise private email field,       // and they don't need to be restricted to only active users.       $query->fields('users', ['mail']);
      $query->condition($query->orConditionGroup()
        ->condition('name', '%' . $keys . '%', 'LIKE')
        ->condition('mail', '%' . $keys . '%', 'LIKE')
      );
    }
    else {
      // Regular users can only search via usernames, and we do not show them       // blocked accounts.       $query->condition('name', '%' . $keys . '%', 'LIKE')
        ->condition('status', 1);
    }
    $uids = $query
      
class FieldLabelDescriptionTranslation extends DrupalSqlBase {

  /** * {@inheritdoc} */
  public function query() {
    // Get translations for field labels and descriptions.     $query = $this->select('i18n_strings', 'i18n')
      ->fields('i18n', ['property', 'objectid', 'type'])
      ->fields('lt', ['lid', 'translation', 'language'])
      ->condition('i18n.type', 'field');
    $condition = $query->orConditionGroup()
      ->condition('property', 'widget_label')
      ->condition('property', 'widget_description');
    $query->condition($condition);
    $query->innerJoin('locales_target', 'lt', '[lt].[lid] = [i18n].[lid]');

    return $query;
  }

  /** * {@inheritdoc} */
  
$select->addField('entity_table', $langcode, 'langcode');
    }
    else {
      $select->addExpression(':langcode', 'langcode', [':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
    }

    // Add the delta column and set it to 0 because we are only dealing with     // single cardinality fields.     $select->addExpression(':delta', 'delta', [':delta' => 0]);

    // Add all the dynamic field columns.     $or = $select->orConditionGroup();
    foreach ($shared_table_field_columns as $field_column_name => $schema_column_name) {
      $select->addField('entity_table', $schema_column_name$dedicated_table_field_columns[$field_column_name]);
      $or->isNotNull('entity_table.' . $schema_column_name);
    }
    $select->condition($or);

    // Lock the table rows.     $select->forUpdate(TRUE);

    return $select;
  }

  

  protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition$batch_size) {
    // Check whether the whole field storage definition is gone, or just some     // bundle fields.     $storage_definition = $field_definition->getFieldStorageDefinition();
    $table_mapping = $this->getTableMapping();
    $table_name = $table_mapping->getDedicatedDataTableName($storage_definition$storage_definition->isDeleted());

    // Get the entities which we want to purge first.     $entity_query = $this->database->select($table_name, 't', ['fetch' => \PDO::FETCH_ASSOC]);
    $or = $entity_query->orConditionGroup();
    foreach ($storage_definition->getColumns() as $column_name => $data) {
      $or->isNotNull($table_mapping->getFieldColumnName($storage_definition$column_name));
    }
    $entity_query
      ->distinct(TRUE)
      ->fields('t', ['entity_id'])
      ->condition('bundle', $field_definition->getTargetBundle())
      ->range(0, $batch_size);

    // Create a map of field data table column names to field column names.     $column_map = [];
    

  public function query() {
    $query = $this->select('menu_links', 'ml')
      ->fields('ml')
      // Shortcut set links are migrated by the d7_shortcut migration.       // Shortcuts are not used in Drupal 6.       // @see Drupal\shortcut\Plugin\migrate\source\d7\Shortcut::query()       ->condition('ml.menu_name', 'shortcut-set-%', 'NOT LIKE');
    $and = $query->andConditionGroup()
      ->condition('ml.module', 'menu')
      ->condition('ml.router_path', ['admin/build/menu-customize/%', 'admin/structure/menu/manage/%'], 'NOT IN');
    $condition = $query->orConditionGroup()
      ->condition('ml.customized', 1)
      ->condition($and);
    $query->condition($condition);
    if (isset($this->configuration['menu_name'])) {
      $query->condition('ml.menu_name', (array) $this->configuration['menu_name'], 'IN');
    }
    $query->leftJoin('menu_links', 'pl', '[ml].[plid] = [pl].[mlid]');
    $query->addField('pl', 'link_path', 'parent_link_path');
    $query->orderBy('ml.depth');
    $query->orderby('ml.mlid');
    return $query;
  }
$query->addExpression('1');
    // Only interested for granting in the current operation.     $query->condition('grant_' . $operation, 1, '>=');
    // Check for grants for this node and the correct langcode.     $nids = $query->andConditionGroup()
      ->condition('nid', $node->id())
      ->condition('langcode', $node->language()->getId());
    // If the node is published, also take the default grant into account. The     // default is saved with a node ID of 0.     $status = $node->isPublished();
    if ($status) {
      $nids = $query->orConditionGroup()
        ->condition($nids)
        ->condition('nid', 0);
    }
    $query->condition($nids);
    $query->range(0, 1);

    $grants = $this->buildGrantsQueryCondition(node_access_grants($operation$account));

    if (count($grants) > 0) {
      $query->condition($grants);
    }

    
$cloned_query = clone $query;
    $cloned_query
      ->condition("$figures.shape", 'circle');
    // Bit 0 (1, 3, 5, 7, 9, 11, 13, 15) needs to be set.     $this->queryResults = $query->execute();
    $this->assertResult(1, 3, 5, 7, 9, 11, 13, 15);
    // No red color has a circle shape.     $this->queryResults = $cloned_query->execute();
    $this->assertResult();

    $query = $this->storage->getQuery()->accessCheck(FALSE);
    $group = $query->orConditionGroup()
      ->exists($greetings, 'tr')
      ->condition("$figures.color", 'red');
    $this->queryResults = $query
      ->condition($group)
      ->condition("$greetings.value", 'sie', 'STARTS_WITH')
      ->sort('revision_id')
      ->execute();
    // Bit 3 and (bit 0 or 2) -- the above 8 part of the above.     $this->assertResult(9, 11, 12, 13, 14, 15);

    // No figure has both the colors blue and red at the same time.
// The rules for determining what conditions to add to the query are as       // follows (applying first applicable rule):       // 1. If the map is joinable, join it. We will want to accept all rows       // which are either not in the map, or marked in the map as NEEDS_UPDATE.       // Note that if high water fields are in play, we want to accept all rows       // above the high water mark in addition to those selected by the map       // conditions, so we need to OR them together (but AND with any existing       // conditions in the query). So, ultimately the SQL condition will look       // like (original conditions) AND (map IS NULL OR map needs update       // OR above high water).       $conditions = $this->query->orConditionGroup();
      $condition_added = FALSE;
      $added_fields = [];
      if ($this->mapJoinable()) {
        // Build the join to the map table. Because the source key could have         // multiple fields, we need to build things up.         $count = 1;
        $map_join = '';
        $delimiter = '';
        foreach ($this->getIds() as $field_name => $field_schema) {
          if (isset($field_schema['alias'])) {
            $field_name = $field_schema['alias'] . '.' . $this->query->escapeField($field_name);
          }
Home | Imprint | This part of the site doesn't use cookies.