Update a CF stored in a custom table, based on another CF

Support MB Custom Table Update a CF stored in a custom table, based on another CFResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #38047
    LauGauLauGau
    Participant

    Hi dear support,

    I have 2 custom fields: "quota_usage" and "quota_monthly" in a custom "websites" table.
    In fine I want to create a CRON job to reset every month the "quota_usage" with the value of "quota_monthly" of each post (note that it's a CPT).

    Now I am just testing to update the data "manually" like so :

    
    add_action( 'init', function() {
    
        $table_name = 'websites';
        $post_id = 532;
        
        // we get the monthly credit
        $quota_monthly = rwmb_get_value( 'quota_monthly', ['storage_type' => 'custom_table', 'table' => $table_name], $post_id );
    
        // we reset the quota_usage with the quota_monthly credit
        $field_id = 'quota_usage';
        $value = $quota_monthly;
        rwmb_set_meta( $post_id, $field_id, $value, [
            'storage_type' => 'custom_table',
            'table'        => $table_name,
        ] );
        
    }, 99 );
    

    When I look at the outputted value it looks good but on the backoffice I still see the previous value in the input... My guess is that the value is not properly saved in the DB...

    What am I doing wrong?

    Thanks for your help 🙂

    #38065
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Can you please share the code that creates the custom fields and custom table on your site? I will check it on my demo site.

    You can also try to use the custom table APIs to add/update data, please read more on the documentation https://docs.metabox.io/extensions/mb-custom-table/#api

    #38069
    LauGauLauGau
    Participant

    Xin Chao Long,

    Actually, I am using the Meta Box UI to declare those :
    https://share.cleanshot.com/W2CH8urnV772nFGLkt4F

    This is the current code:

    
    add_action( 'init', function() {
    
        $table_name = 'websites';
        $post_id = 532; // test on a specific post
        
        // we get the monthly credit
        $time_credit_monthly = rwmb_get_value( 'time_credit_monthly', [
            'storage_type' => 'custom_table',
            'table'        => $table_name],
            $post_id );
    
        // we reset the quota with the monthly credit
        $field_id = 'time_credit_left';
        $value = $time_credit_monthly;
        rwmb_set_meta( $post_id, $field_id, $value, [
            'storage_type' => 'custom_table',
            'table'        => $table_name,
        ] );
            
    }, 99 );
    

    Thanks again for your help.
    In the same time I will have a look at the Custom Table API.

    #38070
    LauGauLauGau
    Participant

    Just tested and it's working with the custom table API:

    
    $object_id = 532;
    $table = 'websites';
    
    $exists = \MetaBox\CustomTable\API::exists( $object_id, $table );
    
    if ( $exists ) {
        
        // Get the monthly quota
        $data = \MetaBox\CustomTable\API::get( $object_id, $table );
        $quota_monthly = $data['time_credit_monthly'];
        
        // Update the quota usage
        $data['time_credit_left'] = $quota_monthly;
        // and we re-save the data
        \MetaBox\CustomTable\API::update( $object_id, $table, $data );
    }
    

    So there is definitely a weird behavior on the rwmb_set_meta() function.

    #38085
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Thanks for your confirmation.

    So please use the custom table APIs instead of the helper function rwmb_set_meta(), I will inform the development team to check it later.

    #39608
    mossmanmossman
    Participant

    Hello, I am using the last version of Meta box and Meta box AIO and the function rwmb_set_meta() does not works for custom tables, only works the custom table API.

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