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
- This topic has 5 replies, 3 voices, and was last updated 2 years, 5 months ago by
mossman.
-
AuthorPosts
-
August 25, 2022 at 5:34 PM #38047
LauGau
ParticipantHi 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 🙂
August 26, 2022 at 10:25 AM #38065Long Nguyen
ModeratorHi,
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
August 26, 2022 at 10:44 AM #38069LauGau
ParticipantXin Chao Long,
Actually, I am using the Meta Box UI to declare those :
https://share.cleanshot.com/W2CH8urnV772nFGLkt4FThis 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.August 26, 2022 at 11:31 AM #38070LauGau
ParticipantJust 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.
August 27, 2022 at 1:21 PM #38085Long Nguyen
ModeratorHi,
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.December 8, 2022 at 7:12 PM #39608mossman
ParticipantHello, 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.
-
AuthorPosts
- You must be logged in to reply to this topic.