Skip Navigation

[Resolved] Disconnecting Relations on PHP-Level

This support ticket is created 4 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by Luo Yang 4 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1905819

Tell us what you are trying to do?
I'm using the "Post Relationships API" to connect Toolset-CP out of JSON-data:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
When getting new JSON-data I'd like to delete all connections for a toolset-relation-slug.
But the disconnect-API-function needs the PostIDs of the two connected CP.
So I'd have to loop through all CP that might have a connection and delete all possible combinations .
This is not good if you have 1000s of CP.
So is there something like
toolset_disconnect_posts("ts-relation-slug") which dumps all relations / connections for a ts-relation?

Thank You
Bernhard

#1906111

Hello,

As the document you mentioned above:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

There isn't such kind of built-in feature within Toolset plugins:
toolset_disconnect_posts("ts-relation-slug")

you can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Our developers will evaluate it

#1906199

Hello Luo,

ok, not good but now I know that I dod't miss it. As I'm able to develop that on my own, can you help me with the database, please?
Is it this way:
Toolset-relation is stored only in the 2 tables "toolset_connected_elements" and "toolset_associations":
At "_toolset_associations" I can get all parent_id and child_id. Those numbers are the group_id at table "toolset_connected_elements" where element_id is pageID.
Is it like this? If so I can develop that.

Thank you,
Bernhard

#1906225

Please provide detail steps to reproduce the problem, how do you setup the post type relationship? I need to test it in my localhost, thanks

#1906253

HI,

I create the relation between posts out of JSON-data with the TS-API-function toolset_connect_posts(...).
That's no problem.
But in the next run, when new or altert JSON comes along, I'd like to delete all relations first and then create the relation between posts from scratch.

The following code should do it, but I'm not sure if I'm missing something or it can be done more performant:

private function clean_toolset_assocations_list() {
global $wpdb;
$sql = "SELECT parent_id, child_id FROM ".$wpdb->prefix."toolset_associations WHERE relationship_id=".$this->toolset_relation_id;
$ts_AssocArr = $wpdb->get_results($sql);
foreach ( $ts_AssocArr as $associtem ) {
$sql = "SELECT element_id FROM ".$wpdb->prefix."toolset_connected_elements WHERE group_id=".$associtem->child_id;
$postidChildHandle = $wpdb->get_results($sql);
$postIdChild = $postidChildHandle[0]->element_id;

$sql = "SELECT element_id FROM ".$wpdb->prefix."toolset_connected_elements WHERE group_id=".$associtem->parent_id;
$postidParentHandle = $wpdb->get_results($sql);
$postIdParent = $postidParentHandle[0]->element_id;

toolset_disconnect_posts($this->toolset_relation_name, $postIdParent, $postIdChild);
}
}

Thank you
Bernhard

#1908785

You did not answer my question: how do you setup the post type relationship?

Are we talking about one-to-many relationship or many-to-many relationship?
Please take a screenshot for how do you setup the post type relationship, as I mentioned above, I need to test it in my localhost, thanks

#1908805
ts-rel.jpg

It's a many-to-many-relation without creating intermediary post types
Attached a screenshort of the relation.

Thank you

#1908933

It is not recommended to use custom codes to delete the post type relationship, if I provide you some custom codes for this issue, it might not work in the future version of Toolset plugins.

In your case, there is a simple workaround:
You can recreate the many-to-many relationship "major-hazard2-hazprod2", enable option "Create an intermediary post type", so each time when you want to delete those relationships, you just need to:
1) Find all intermediate "major-hazard2-hazprod2" posts
https://developer.wordpress.org/reference/functions/get_posts/

2) remove all "major-hazard2-hazprod2" posts with WordPress function wp_delete_post()
https://developer.wordpress.org/reference/functions/wp_delete_post/

It should be able to disconnect all posts too

#1909915

Hi,

sure it's not the best idea to delete the relations in this way, as Toolset might change that again.
But I guess creating 1000s intermediate posts and then deleting it takes some time.
I'll check that.
Can you pass that need to delete all relations of a relation_id via an API-function to your developers?

Thank you
Bernhard

#1910057

OK, I will pass it as a feature request to our developers.

#1916323

Thank you!
As there is a demang from several of my customers I developed a plugin for that:
hidden link

Best would be that this plugin is not needed and the feature is part of toolset.

Bernhard

#1917113

The feature request had already submitted, our developers will evaluate it, but there isn't any ETA for it.

Currently, please try the workaround I mentioned above:
https://toolset.com/forums/topic/disconnecting-relations-on-php-level/#post-1908933