[wp-trac] [WordPress Trac] #22196: add $wpdb->upsert function
WordPress Trac
wp-trac at lists.automattic.com
Mon Oct 15 14:53:24 UTC 2012
#22196: add $wpdb->upsert function
-------------------------+------------------------------
Reporter: thomask | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Database | Version: 3.4.2
Severity: normal | Resolution:
Keywords: |
-------------------------+------------------------------
Comment (by wonderboymusic):
This one would be tricky:
Inserts and Updates also take formatting as an argument. If you pass the
primary key as an arbitrary value, the primary key and its formatting
would have to be removed from the values AND formatting arrays, regardless
of what order they were passed.
The resulting SQL needs to look like:
{{{
INSERT INTO wp_posts (post_title, post_date, ID, post_content, post_type)
VALUES ('Title', '2012-10-07 07:00:00', 3, 'This is content', 'event')
ON DUPLICATE KEY UPDATE
post_title = 'Title',
post_date ='2012-10-07 07:00:00',
post_content = 'This is content',
post_type = 'event'
}}}
But that can be passed to {{{$wpdb->insert}}} like so:
{{{
$wpdb->insert(
'wp_posts'
array(
'post_title' => 'Title',
'post_date' => '2012-10-07 07:00:00',
'ID' => 3,
'post_content' => 'This is content',
'post_type' => 'event'
),
array( '%s', '%s', '%d', '%s', '%s' )
);
}}}
The insert method takes arbitrary arrays as arguments, but to remove the
*specific* primary key for any passed table, it would have to be
intercepted and altered just in time to create the proper update SQL.
I'm not sure wpdb should get this fancy. Semi-related:
https://core.trac.wordpress.org/ticket/19019
--
Ticket URL: <http://core.trac.wordpress.org/ticket/22196#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list