WPSEO_Database_Proxy::upsert()publicYoast 1.0

Upserts data in the database.

Performs an insert into and if key is duplicate it will update the existing record.

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

Хуков нет.

Возвращает

false|int. False when the upsert request is invalid, int on number of rows changed.

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

$WPSEO_Database_Proxy = new WPSEO_Database_Proxy();
$WPSEO_Database_Proxy->upsert( $data, ?array $where, $format, $where_format );
$data(массив) (обязательный)
Data to update on the table.
?array $where **
-
По умолчанию: null
$format(массив|строка|null)
Data prepare format.
По умолчанию: null
$where_format(массив|строка|null)
Where prepare format.
По умолчанию: null

Код WPSEO_Database_Proxy::upsert() Yoast 22.4

public function upsert( array $data, ?array $where = null, $format = null, $where_format = null ) {
	if ( $where_format !== null ) {
		_deprecated_argument( __METHOD__, '7.7.0', 'The where_format argument is deprecated' );
	}

	$this->pre_execution();

	$update  = [];
	$keys    = [];
	$columns = array_keys( $data );
	foreach ( $columns as $column ) {
		$keys[]   = '`' . $column . '`';
		$update[] = sprintf( '`%1$s` = VALUES(`%1$s`)', $column );
	}

	$query = sprintf(
		'INSERT INTO `%1$s` (%2$s) VALUES ( %3$s ) ON DUPLICATE KEY UPDATE %4$s',
		$this->get_table_name(),
		implode( ', ', $keys ),
		implode( ', ', array_fill( 0, count( $data ), '%s' ) ),
		implode( ', ', $update )
	);

	$result = $this->database->query(
		$this->database->prepare(
			$query,
			array_values( $data )
		)
	);

	$this->post_execution();

	return $result;
}