Support Forum
Support › MB Custom Table › Update a CF stored in a custom table, based on another CFResolved
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 🙂
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
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.
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.
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.
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.