Custom table should not try to delete cache if Objec_id is null

Support MB Custom Table Custom table should not try to delete cache if Objec_id is null

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #45890
    thomassultanathomassultana
    Participant

    We have a situation where we write data to a metabox custom table but we dont specify a primary key, we allow the increment to autogenerate an id. This means that our object_id is null. Whenever we call \MetaBox\CustomTable\API::add( null...)

    The following tries to occur:

    public static function add( ?int $object_id, string $table, array $row ) {
    global $wpdb;
    $row['ID'] = $object_id;
    $row = array_map( function ($item) {
    return self::maybe_serialize( $item );
    }, $row );
    $row = apply_filters( 'mbct_add_data', $row, $object_id, $table );
    do_action( 'mbct_before_add', $object_id, $table, $row );
    $output = $wpdb->insert( $table, $row );
    do_action( 'mbct_after_add', $object_id, $table, $row );

    Cache::delete( $object_id, $table );

    return $output;
    }

    Maybe Cache::delete should run if $object_id is not null?

    #45894
    PeterPeter
    Moderator

    Hello Thomas,

    Yes, the delete cache function will do nothing if the variable $object_id is null. You can take a look at this file /mb-custom-table/src/Cache.php line 59

    public static function delete( ?int $object_id, string $table ) {
    	if ( ! $object_id ) {
    		return;
    	}
    
    	wp_cache_delete( $object_id, self::get_cache_group( $table ) );
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.