mbct_after_add $object_id empty

Support MB Custom Table mbct_after_add $object_id empty

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #35814
    wgstjfwgstjf
    Participant

    Hi guys,

    Quick one - we're trying to use the mbct_after_update hook on a Custom Model but the retrieved $object_id variable is always empty.

    https://docs.metabox.io/extensions/mb-custom-table/#hooks

    Is this a bug that you can create at your end? Is so any chance you could push through a quick fix?

    Cheers,

    Will

    #35822
    Long NguyenLong Nguyen
    Moderator

    Hi Will,

    What is the code that you are using to use the $object_id? I've tested this action with a callback function and it displays the object ID as well.

    add_action( 'mbct_after_update', function( $object_id, $table, $row ) {
        echo "Object ID is: " . $object_id . '<br>';
        die( 'Stop here!' ); //for debug
    }, 10, 3);

    And make sure that you have the latest version of MB Custom Table 2.1.1.

    #35828
    wgstjfwgstjf
    Participant

    Apologies, It was the mbct_after_add not mbct_after_update hook that had the issue. I referenced the wrong hook in the body of the original post

    Would you mind checking again for me?

    #35837
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The object ID is set to null and uses the auto-increment number of the column ID in the custom table so I think it will not work when you use the custom model. I will inform the development team to check this case and get back to you later.

    #35857
    wgstjfwgstjf
    Participant

    Thank, I look forward to hearing how to get the ID of the just added item.

    Cheers,

    Will

    #35871
    wgstjfwgstjf
    Participant

    Hi,

    Possible solution for the above issue...

    
    public function insert_row( $row ) {
    	global $wpdb;
    	$id = isset( $row['ID'] ) ? $row['ID'] : null;
    	do_action( 'mbct_before_add', $id, $this->table, $row );
    	$output = $wpdb->insert( $this->table, $row );
    	do_action( 'mbct_after_add', $wpdb->insert_id, $this->table, $row ); // Change $id to $wpdb->insert_id
    	return $output;
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.