useDefaults example

        $connection = Database::getConnection('default', 'default');
      }
    }
    return $connection;
  }

  /** * {@inheritdoc} */
  public function createNew(): int|string {
    return $this->connection->insert('simpletest_test_id')
      ->useDefaults(['test_id'])
      ->execute();
  }

  /** * {@inheritdoc} */
  public function setDatabasePrefix(TestRun $test_run, string $database_prefix): void {
    $affected_rows = $this->connection->update('simpletest_test_id')
      ->fields(['last_prefix' => $database_prefix])
      ->condition('test_id', $test_run->id())
      ->execute();
    
'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
        'test_nullable_field' => ['type' => 'int', 'not null' => FALSE],
      ],
      'primary key' => ['serial_column'],
    ];
    $this->schema->createTable($table_name$table_spec);

    // Insert some rows to the table to test the handling of initial values.     for ($i = 0; $i < 3; $i++) {
      $this->connection
        ->insert($table_name)
        ->useDefaults(['serial_column'])
        ->fields(['test_nullable_field' => 100])
        ->execute();
    }

    // Add another row with no value for the 'test_nullable_field' column.     $this->connection
      ->insert($table_name)
      ->useDefaults(['serial_column'])
      ->execute();

    $this->schema->addField($table_name, 'test_field', $field_spec);

    

class InsertDefaultsTest extends DatabaseTestBase {

  /** * Tests that we can run a query that uses default values for everything. * * @see \database_test_schema() */
  public function testDefaultInsert() {
    $query = $this->connection->insert('test')->useDefaults(['job']);
    $id = $query->execute();
    $job = $this->connection->query('SELECT [job] FROM {test} WHERE [id] = :id', [':id' => $id])->fetchField();
    $this->assertSame('Undefined', $job, 'Default field value is set.');
  }

  /** * Tests that no action will be preformed if no fields are specified. */
  public function testDefaultEmptyInsert() {
    $num_records_before = (int) $this->connection->query('SELECT COUNT(*) FROM {test}')->fetchField();

    
throw new InvalidMergeQueryException('Invalid merge query: no conditions');
    }

    $select = $this->connection->select($this->conditionTable)
      ->condition($this->condition);
    $select->addExpression('1');

    if (!$select->execute()->fetchField()) {
      try {
        $insert = $this->connection->insert($this->table)->fields($this->insertFields);
        if ($this->defaultFields) {
          $insert->useDefaults($this->defaultFields);
        }
        $insert->execute();
        return self::STATUS_INSERT;
      }
      catch (IntegrityConstraintViolationException $e) {
        // The insert query failed, maybe it's because a racing insert query         // beat us in inserting the same row. Retry the select query, if it         // returns a row, ignore the error and continue with the update         // query below.         if (!$select->execute()->fetchField()) {
          throw $e;
        }
Home | Imprint | This part of the site doesn't use cookies.