Automattic\WooCommerce\Internal\Utilities

DatabaseUtil::insert_on_duplicate_key_update()publicWC 1.0

Hybrid of $wpdb->update and $wpdb->insert. It will try to update a row, and if it doesn't exist, it will insert it. This needs unique constraints to be set on the table on all ID columns.

You can use this function only when:

  1. There is only one unique constraint on the table. The constraint can contain multiple columns, but it must be the only one unique constraint.
  2. The complete unique constraint must be part of the $data array.
  3. You do not need the LAST_INSERT_ID() value.

Метод класса: DatabaseUtil{}

Хуков нет.

Возвращает

int. Returns the value of DB's ON DUPLICATE KEY UPDATE clause.

Использование

$DatabaseUtil = new DatabaseUtil();
$DatabaseUtil->insert_on_duplicate_key_update( $table_name, $data, $format ) : int;
$table_name(строка) (обязательный)
Table name.
$data(массив) (обязательный)
Unescaped data to update (in column => value pairs).
$format(массив) (обязательный)
An array of formats to be mapped to each of the values in $data.

Код DatabaseUtil::insert_on_duplicate_key_update() WC 7.1.0

public function insert_on_duplicate_key_update( $table_name, $data, $format ) : int {
	global $wpdb;

	$columns             = array_keys( $data );
	$column_clause       = '`' . implode( '`, `', $columns ) . '`';
	$value_placeholders  = implode( ', ', array_values( $format ) );
	$on_duplicate_clause = $this->generate_on_duplicate_statement_clause( $columns );
	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Values are escaped in $wpdb->prepare.
	$sql = $wpdb->prepare(
		"
INSERT INTO $table_name ( $column_clause )
VALUES ( $value_placeholders )
$on_duplicate_clause
",
		array_values( $data )
	);
	// phpcs:enable
	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is prepared.
	return $wpdb->query( $sql );
}