我有一种情况,需要将几个条目添加到数据库表中(有时是几百个),但使用$wpdb->insert()
(和$wpdb->update()
在需要编辑的地方)一次运行一个插入,这是非常低效的。
是否有$wpdb
方法,该方法允许我通过循环查询来创建查询,然后在退出该循环后执行它?
下面是我当前用于添加新数据的代码。如有任何改进建议,将不胜感激-
function do_add_invitees_in_database(){
/** Make sure there are invitees to add */
if(empty($this->invitees[\'new\'])) :
return $_POST[\'add_successfull\'] = true;
endif;
global $wpdb;
/** Get the creator ID */
$creator_id = get_current_user_id();
/** Make the invitee creation date */
$date_added = date(\'Y-m-d H:i:s\', time());
/** Insert the new invitees in to the \'event_invitees\' database table */
foreach($this->invitees[\'new\'] as $invitee) :
$row = array(
\'invitee_created_by\' => $creator_id,
\'invitee_created_date\' => $date_added,
\'invitee_last_edited_by\' => \'0\',
\'invitee_last_edited_date\' => \'0000-00-00 00:00:00\',
\'event_id\' => $_POST[\'event_id\'],
\'email\' => $invitee[\'email\'],
\'first_name\' => $invitee[\'first_name\'],
\'surname\' => $invitee[\'surname\'],
\'custom\' => $invitee[\'custom\'],
\'invited\' => 0
);
$wpdb->insert($wpdb->event_invitees, $row);
/** Debug the query run by $wpdb->insert() */
$this->debug($row);
endforeach;
/** If there were no errors, mark the invitee additions as successfull */
return $_POST[\'add_successfull\'] = (!$this->error_found) ? true : false;
}