Codeception\Module\ExtendedDb::haveOrUpdateInDatabase PHP Method

haveOrUpdateInDatabase() public method

Inserts or updates a database entry on duplicate key.
public haveOrUpdateInDatabase ( string $table, array $data ) : void
$table string The table name.
$data array An associative array of the column names and values to insert.
return void
    public function haveOrUpdateInDatabase($table, array $data)
    {
        $query = $this->driver->insertOrUpdate($table, $data);
        $this->debugSection('Query', $query);
        $sth = $this->driver->getDbh()->prepare($query);
        if (!$sth) {
            $this->fail("Query '{$query}' can't be executed.");
        }
        $i = 1;
        foreach ($data as $val) {
            $sth->bindValue($i, $val);
            $i++;
        }
        $res = $sth->execute();
        if (!$res) {
            $this->fail(sprintf("Record with %s couldn't be inserted into %s", json_encode($data), $table));
        }
    }

Usage Example

Example #1
0
 /**
  * Inserts or updates a database entry.
  *
  * An override of the parent method to allow back compatibililty and configuration based use.
  *
  * @param  string $table The table name.
  * @param  array $data An associative array of the column names and values to insert.
  *
  * @return void
  */
 public function javeInDatabase($table, array $data)
 {
     $this->debugSection('Configuration', sprintf('Update setting set to %s', $this->config['update']));
     if (isset($this->config['update']) and $this->config['update']) {
         return parent::haveOrUpdateInDatabase($table, $data);
     }
     return parent::haveInDatabase($table, $data);
 }