\Drupal::database() ->insert - Error: Call to undefined method Using ->condition()? I am trying to insert a new row based on conditions. The -&gt;condition() in $database-&gt;update() works fine, in $database-&gt;insert() they do not.

$database = \Drupal::database();

// Query our DB.
$queryDB = $database
  -&gt;select('table_name', 'value3')
  -&gt;fields('value3')
  -&gt;condition('value1', $value1, "=")
  -&gt;condition('value2', $value2, "=")
  -&gt;execute()-&gt;fetchAll();

if (isset($queryDB[0])) {
  // Check if table row exists and overwrite.
  if ($queryDB[0]-&gt;value1 == $value1 &amp;&amp; $queryDB[0]-&gt;value2 == $value2) {
    $database
      -&gt;update('table_name')
      -&gt;fields([
        'value3' =&gt; $value3,
      ])
      -&gt;condition('value1', $value1, '=')
      -&gt;condition('value2', $value2, '=')
      -&gt;execute();
  }
}
// Create table row.
else {
  $database
    -&gt;insert('table_name')
    -&gt;fields([
      'value1' =&gt; $value1,
      'value2' =&gt; $value2,
      'value3' =&gt; $value3,
    ])
    // Without these conditions, I get a duplicate entry integrity restraint as value1 and/or value2 may already exist.
    // With them I get Error: Call to undefined method Drupal\Core\Database\Driver\mysql\Insert::condition().
    -&gt;condition('value1', $value1, '!=')
    -&gt;condition('value2', $value2, '!=')
    -&gt;execute();
}


Very similar questions: 
1. EntityQuery condition &quot;is not equal to...&quot;
2. How do I add a condition to a query?

But I am having no luck. Is it different with an insert?

What I want is that if value1 AND value2 don't both match, create an entirely new table row with all three values.
As described in Insert Queries, the object returned from \Drupal::database()-&gt;insert() doesn't have an condition() method. If the purpose of the code is creating a new database row if it doesn't exist, or update it if it already exists, 
the code should use the object returned from \Drupal::database()-&gt;merge(). See Merge Queries.

For example, the following code will create a new row if none of the existing rows contains the value $name for the name database column; otherwise, it will update the existing matching row. ($connection is the object returned from \Drupal::database() or a Connection object obtained via dependency injection.)

$connection-&gt;merge('example')
  -&gt;key(['name' =&gt; $name])
  -&gt;fields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;execute();


It is also possible to use a set of values when the row is created, and another set when the row is updated. 

$connection-&gt;merge('example')
  -&gt;insertFields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;updateFields([
    'field1' =&gt; $alternate1,
  ])
  -&gt;key(['name' =&gt; $name])
  -&gt;execute();


What that documentation page doesn't show is how to use multiple keys, as the following code does, using Merge::keys() instead of Merge::key().

$connection-&gt;merge('example')
  -&gt;insertFields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;updateFields([
    'field1' =&gt; $alternate1,
  ])
  -&gt;keys(['name' =&gt; $name, 'nickname' =&gt; $nickname])
  -&gt;execute();


The only limitation is that the values passed to keys() are all used to match a single database row. In other words, the last snippet I shown doesn't look for a row containing $name in the name database column or a row containing $nickname in the nickname column.

In cases more complex, where the Merge class doesn't help, you need to first check what the database table contains and take the necessary action basing on the table content (which involves writing more PHP code to check the database content).