API add method

Support MB Custom Table API add method

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #43618
    badabingbredabadabingbreda
    Participant

    Hi there,
    I ran into an issue where I insert a new row using the
    \MetaBox\CustomTable\API::add( $object_id, $table, $data ); method. I was expecting it to return the newly created row ID, but it returns the number of rows per WP docs. The output for the method is the result for $wpdb->insert().
    The workaround is using global $wpdb in the function call and using $wpdb->insert_id to get the latest inserted ID, but I just wanted to mention that I found it more logical that the add methoud would return the row ID or false if things didn't work out.

    #43647
    PeterPeter
    Moderator

    Hello,

    Can you please share a/some cases that if things didn't work out? Also, there is a note in the source code about the custom model:

    Set $object_id to null for auto-increment table (for models).

    #43652
    badabingbredabadabingbreda
    Participant

    I'd like to think the method is comparable to wp_insert_post() to insert a record, if the goal is to insert/add a auto-incremented row. But the MetaBox\CustomTable\API::add() returns the number of records returned (which I guess will always be 1 if successful, 0 if unsuccessful for whatever reason) and the wp_insert_post() method returns the newly created ID or a WP_Error object.

    I was expecting the MetaBox add method to return the newly created ID, but found that it didn't. I think returning either an ID or false (not successful) would be returning more valuable information.

    My use case is that I use custom tables to create a model type where I store results for a taken assessment (newly created on submission, need the created ID) and immediately need that ID to store the meta (also custom table) for an arbitrary amount of question results, so not stored in columns but assessmentid,key,value format.

    This marked as a non-support question, so it's rather a 'wouldn't it be more effective to return the created row ID' kinda topic.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.