How to get the table name given the field_id
Support › MB Custom Table › How to get the table name given the field_idResolved
- This topic has 5 replies, 2 voices, and was last updated 5 years, 2 months ago by
Anh Tran.
-
AuthorPosts
-
January 24, 2020 at 6:56 PM #18000
COMCEPT
ParticipantHello,
I know thatrwmb_get_field_settings()
returns an array and in['storage']
" we get an instance ofRWMB_Table_Storage
. Is there a reason why the$table
property is protected? Can we get something likerwmb_get_field_custom_table()
?As of now I'm relying on the array casting trick.
function get_property(object $object, string $property) { $array = (array) $object; $propertyLength = strlen($property); foreach ($array as $key => $value) { if (substr($key, -$propertyLength) === $property) { return $value; } } }
So this is how I get the table name of a field.
var_dump( get_property ( rwmb_get_field_settings( $meta_key, '', $object_id )['storage'], 'table' ) );
This code smells though.
Thanks,
NickJanuary 25, 2020 at 7:50 PM #18011Anh Tran
KeymasterHi Nick, the reason we make the property protected is because we don't want people to change it.
Can you tell me why do you want to the the table name? Is that the table you created with your own code? Why don't you just pass the string (table name) instead of getting from fields?
January 25, 2020 at 9:53 PM #18013COMCEPT
ParticipantThanks Anh, of course I can tell 🙂
So we have these custom post-types and they all share common data; we then decided to store this shared data in a common custom table and some specific data in their respective post-type-specific custom tables.
We're hooking into the
get_post_metadata
and theadd_post_metadata
so that we can generalize the usage of the custom table plugin and render it "transparent" to other plugins such as Polylang.To do so we need to programmatically get the source/destination table, given a meta-key, where we need to retrieve/store its value.
Let's focus on writing data (because we just join the shared/common meta-data table upon reading so it's simpler).
Our meta writing function, in pseudo-code, looks more or less like this
add_filter('add_post_metadata', function ( $check, $object_id, $meta_key, $meta_value, $unique ){ if this_is_not_a_custom_post_type || the_meta_key_is_not_a_metabox_field return null; // This is where we select the table, based on the field_id/meta_key $table = com_get_obj_property( rwmb_get_field_settings( $meta_key, '', $object_id )['storage'], 'table' ); // This is so we update an existing ID in the custom table afterwards maybe_insert_id_in_custom_meta_table( $object_id, $table ); // Maybe we need to serialize the value if ( !empty($meta_value) ) $meta_value = maybe_serialize( $meta_value ); // Finally write the correct meta value in the correct table updating the correct ID $wpdb->update( $table, [$meta_key => $meta_value], ['ID' => $object_id] ); });
As you can see, we dynamically need to grab the correct table based on the meta_key. That's why we'd need to have access to it (and we have, via the array casting trick).
A simple getter, would be enough. No need to have a setter or make it public.
January 28, 2020 at 9:09 PM #18029Anh Tran
KeymasterHi Nick,
Thanks for your reply. I just noticed I have a setter method public, which is quite simple (and naive). So, it's fine to make the property public. I just released a new version for the extension. Please update.
January 29, 2020 at 6:33 PM #18053COMCEPT
ParticipantThank you,
we have the AIO plugin.
Could you update that as well?January 31, 2020 at 2:33 PM #18075Anh Tran
KeymasterHi Nick, I've just updated AIO. Please try again.
-
AuthorPosts
- You must be logged in to reply to this topic.