mbct_before_update - want to identify if a field has changed
Support › MB Custom Table › mbct_before_update - want to identify if a field has changedResolved
- This topic has 7 replies, 3 voices, and was last updated 1 year ago by
Peter.
-
AuthorPosts
-
March 12, 2024 at 7:45 PM #44817
Ben R
ParticipantI'd like to identify if a given field of data (a status field) has been changed during an update to a custom table.
Hooks here: https://docs.metabox.io/extensions/mb-custom-table/#hooks
The following code does not detect the change. For some reason, the API::get() all shows the status field is the same before being updated.
add_action( 'mbct_before_update', function( $object_id, $table, $row ) { $data = \MetaBox\CustomTable\API::get( $object_id, $table ); // this doesn't work; status is always the same if ($row['status'] != $data['status']) { error_log( 'mbct status changed from ' . $data['status'] . ' to ' . $row['status'] ); } else { error_log( 'mbct status the same ' . $data['status'] . ' ' . $row['status'] ); } }, 10, 3);
I worked around it by making a direct sql query to the table.
add_action( 'mbct_before_update', function( $object_id, $table, $row ) { global $wpdb; $result = $wpdb->get_results( "SELECT * from some_activity_table WHERE id = " . $object_id ); if (sizeof($result) == 1) { $old = $result[0]->status; $new = $row['status']; if ($old != $new) { error_log( 'mbct status changed' ); } else { error_log( 'mbct status the same' ); } } }, 10, 3);
The plugin code looks ok and fires the actions at the correct spots. I see that the ::get() function pulls the record from the Cache::get(). Perhaps that is the culprit? Is there a proper way to accomplish this that I'm overlooking?
For instance, the rwmb_{field_id}_after_save_field is fantastic because it includes $new and $old parameters. https://docs.metabox.io/actions/rwmb-after-save-field/
add_action( 'rwmb_status_after_save_field', function( $null, $field, $new, $old, $object_id) { if ($new != $old) { // detects change } }, 10, 5);
March 13, 2024 at 9:16 PM #44833Peter
ModeratorHello Benjamin,
I checked the code and think you are correct. The data is saved to the cache before updating to the custom table so you will see
$row['status'] = $data['status']
. Using the SQL query to get the current field value in the table is a good way.March 14, 2024 at 12:15 AM #44851Ben R
ParticipantThanks for your help. However, the very nature of this hook is to access the record before it has been updated, so I would expect to be able to accomplish that in an elegant fashion.
I do appreciate the caching - don't get me wrong! But hitting raw sql here seems like a hack to me.
March 14, 2024 at 9:55 PM #44858Peter
ModeratorHello,
I've escalated this case to the development team. We will consider using cache when getting field value from the custom table with the function rwmb_meta() only and the rest will be queried directly from database.
Thank you.
March 14, 2024 at 10:05 PM #44860Ben R
ParticipantThanks much, I appreciate it. I'll use this workaround for now.
By the way, I love your plugin. It's fantastic.
March 22, 2024 at 5:38 AM #44963Ben R
ParticipantI see that there was an update to potentially work around this issue.
Add a $force param to API::get() method to force getting raw data from the database (true: default) or cache (false).
However, this update is now crashing custom tables (specifically a model I have created). When I try to add a new element to my model, I get the following:
[21-Mar-2024 22:35:29 UTC] PHP Fatal error: Uncaught TypeError: MetaBox\CustomTable\Cache::get(): Argument #1 ($object_id) must be of type int, string given, called in /bitnami/wordpress/wp-content/plugins/mb-custom-table/src/Storage.php on line 29 and defined in /bitnami/wordpress/wp-content/plugins/mb-custom-table/src/Cache.php:19 Stack trace: #0 /bitnami/wordpress/wp-content/plugins/mb-custom-table/src/Storage.php(29): MetaBox\CustomTable\Cache::get() #1 /bitnami/wordpress/wp-content/plugins/meta-box/inc/field.php(148): MetaBox\CustomTable\Storage->get() #2 /bitnami/wordpress/wp-content/plugins/meta-box/inc/field.php(535): RWMB_Field::raw_meta() #3 /bitnami/wordpress/wp-content/plugins/meta-box/inc/meta-box.php(328): RWMB_Field::call() #4 /bitnami/wordpress/wp-content/plugins/meta-box/inc/meta-box.php(192): RW_Meta_Box->is_saved() #5 /opt/bitnami/wordpress/wp-admin/includes/template.php(1456): RW_Meta_Box->show() #6 /bitnami/wordpress/wp-content/plugins/mb-custom-table/views/add.php(25): do_meta_boxes() #7 /bitnami/wordpress/wp-content/plugins/mb-custom-table/src/Model/Admin.php(209): include('...') #8 /opt/bitnami/wordpress/wp-includes/class-wp-hook.php(324): MetaBox\CustomTable\Model\Admin->render() #9 /opt/bitnami/wordpress/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #10 /opt/bitnami/wordpress/wp-includes/plugin.php(517): WP_Hook->do_action() #11 /opt/bitnami/wordpress/wp-admin/admin.php(259): do_action() #12 {main} thrown in /bitnami/wordpress/wp-content/plugins/mb-custom-table/src/Cache.php on line 19
March 24, 2024 at 2:59 AM #44979Johannes Gross
ParticipantI am experiencing the same PHP Fatal error as Ben R when adding a new element to a model after updating to version 2.1.9. I then restored version 2.1.8 and it is working again without any issues.
March 25, 2024 at 10:43 PM #45003Peter
ModeratorHello,
Thank you for the feedback. I can see the error on my demo site and I also escalated it to the development team to fix the issue soon.
-
AuthorPosts
- You must be logged in to reply to this topic.