MB_Custom_Table_API: check and add queries

Support MB Custom Table MB_Custom_Table_API: check and add queriesResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33338
    Nicholas CoxNicholas Cox
    Participant

    Hi

    I have read over the docs but slightly confused.

    1. I use the following code but what happens if i have 1000's of rows, do I need to iterate over until there is no match as pass this as the $objectid? is there a way just to check if the table exists rather than checking the row? cant seem to find it on the docs. https://docs.metabox.io/extensions/mb-custom-table/#api

    $data = \MetaBox\CustomTable\API::exists( $object_id, $table );

    1. How come we need to know the row ID ($object_id) when adding a new entry? does it not work like wpdb where you can add a new entry without specifying a row ID?
    
    $data = [
        'field_1' => 'value 1',
        'field_2' => 'value 2',
        'field 3' => ['one', 'two', 'three'],
    ];
    \MetaBox\CustomTable\API::add( $object_id, $table, $data );
    
    #33352
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you want to check the table existed, please refer to this topic https://wordpress.stackexchange.com/questions/11641/checking-if-database-table-exists

    This extension helps you to save the post meta to the custom table, so you can hook to some actions that support using the post ID to pass to the API like save_post
    https://developer.wordpress.org/reference/hooks/save_post/

    For example:

    add_action( 'save_post', 'my_function', 10,3 );
     
    function my_function( $post_id, $post, $update ) {
        $data = [
            'field_1' => 'value 1',
            'field_2' => 'value 2',
            'field 3' => ['one', 'two', 'three'],
        ];
        \MetaBox\CustomTable\API::add( $post_id, 'table_name', $data );
    }
    #33360
    Nicholas CoxNicholas Cox
    Participant

    Ah ok thanks. So in regards to custom models i will still have to check if a database row is available beforehand. I assume ill have to do this via wpdb.

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