WPSEO_Database_Proxy::upsert
Upserts data in the database.
Performs an insert into and if key is duplicate it will update the existing record.
Метод класса: WPSEO_Database_Proxy{}
Хуков нет.
Возвращает
int|false. 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 );
- $data(массив) (обязательный)
- Data to update on the table.
- ?array $where
- .
По умолчанию:null - $format(массив|строка|null)
- Deprecated since 28.6. Unused since the query was rewritten in 7.7.0.
По умолчанию:null
Код WPSEO_Database_Proxy::upsert() WPSEO Database Proxy::upsert Yoast 28.9
public function upsert( array $data, ?array $where = null, $format = null ) {
if ( $where !== null ) {
_deprecated_argument( __METHOD__, 'Yoast SEO 28.6', 'The where argument is deprecated' );
}
if ( $format !== null ) {
_deprecated_argument( __METHOD__, 'Yoast SEO 28.6', 'The 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;
}