Skip Navigation

[Resolved] How to disconnect many to many relationship

This support ticket is created 4 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Tagged: 

This topic contains 15 replies, has 4 voices.

Last updated by nabils 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1406649

Tell us what you are trying to do?
Hi,

I have created a relationship form for many to many relationship. Now I would like to give the user the option to disconnect the relationship. How this can be done?

#1407167

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Thank you for getting in touch.

I'm assuming that you're referring to disconnecting the relationship on the frontend correct?
Unfortunately this is not possible to do as you can only disconnect them through the backend.

What I would suggest that you do is to open a feature request for this so that it can be reviewed by our development team and get added sometime later in the future.

To suggest this feature request I would recommend that you go to the link below.
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Thanks,
Shane

#1408153

Thank you.
Yes on the front end.
In this case I am thinking to create an other post and link the two posts to this post as one to many relationship. In my case I have property post and agent post. The property can have 0 to 3 agents and the agent can have 0 to many properties. So, may be I can add a post called "Property agent" and link property to this post as one to many and do the same with agent post. The I create form for property agent where I can link current property with any agent. However, in this case I my have a duplicate . Is there a way to avoid duplicate?
Can this be a practical solution?

#1408521

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Based on your explanation it would get quite complex because you would essentially edit the property agent cpt to disconnect an Agent from a Property.

It would work essentially since you are using the Property Agent CPT as a kind of link between both and you can use a Form on the frontend to disconnect the associations.

I think it is a practical solution, however when displaying the Agents of a property you will need to use 2 views instead of one.

Where 1 view will be for Property Agent and the second would be for the Agents. So how it would work is that Property is the Parent of Property Agents and Agents is the Child of Property Agents.

So your relationship would look like this Grandparent -> Child -> Grandchild.

Please let me know if this helps.
Thanks,
Shane

#1410247

How can I avoid connecting the same property to the same agent many time in this case?

#1411989

Hi, Shane is out on holiday this week so I am looking into his tickets. I'm a bit concerned about the post relationship approach Shane proposed:
"So how it would work is that Property is the Parent of Property Agents and Agents is the Child of Property Agents. So your relationship would look like this Grandparent -> Child -> Grandchild."

The problem I see here is that in a one-to-many relationship between Property Agent (one) and Agent (many), an Agent can only ever be associated with one Property Agent post. In other words, one Agent cannot have multiple Property Agent parents, hence cannot be associated with multiple Properties. If I understand the scenario correctly, this setup would not be ideal because you want to be able to associate one Agent with many Properties, am I correct? If so, then this type of hierarchical relationship isn't going to work. Agents must be a parent of Property Agents if you want to be able to link Agents with multiple Properties.

Like you already mentioned, you would create a new Form that creates Property Agents. I think the best way to prevent duplicates here is to use custom code with the Forms API cred_form_validate and the Post Relationships API toolset_get_related_posts. The cred_form_validate API will let you trigger some code to validate the selected parent when the Form is submitted. The toolset_get_related_posts API will let you query related posts to determine if the selected parent has already been linked to the current post. If so, you would return an error to the User submitting the Form, and the duplicate would be blocked.

We have documentation available for those APIs here, with example code:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I can help provide examples or troubleshoot Toolset APIs if you need assistance.

#1424405

Can I use toolset_disconnect_posts to disconnect two connected post by many to many relationship in front end?

#1426443

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Christian is on vacation. He will get back to work this Sunday. I'm stepping in, hope this is ok.

Yes - you can use the post-relationship API function toolset_disconnect_posts() to delete the many-to-many connection but you need to find a way to trigger it using some custom shortcode or if you are using post form then you can use the Toolset Form's API hook - cred_save_data.

Please note that there is no API hook available with Toolset post-relationship forms.

For example:

function func_delete_m2m_relationship($atts) {
	 $parent = 607;
	 $child = 608;
	 if(!is_admin()) {
		toolset_disconnect_posts('book-book-author', $parent, $child );
        }
}
add_shortcode("delete_relationship", "func_delete_m2m_relationship");

- Where 'book-book-author' is many-to-many post relationship slug.

As you may know, that connections for many-to-many post-relationship will be stored in the intermediary post type. The intermediary post type is automatically created when you created the many-to-many relationship that will hold the connections.

#1427865

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I shared the solution with my previous reply, please let me know if you need further assistance.

#1429685

Hi,
It shows:
Fatal error: Uncaught Error: Call to undefined function add_shortcode()

#1429689

I have moved it to toolset custom code and the problem was fixed. However, I want to know where I have to add the shortcode. Can I add it in view ( I have a view that shows all agents related to one property) as a link next to each agent. If yes then what will be
$parent = ?;
$child = ?;

#1431347

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Its still not clear to me. At what point/page/action you want to disconnect the posts? Can you please clarify?

#1432311

In the content template of property I display a view of all agents connected to the property. Can I add a remove link (in this case the shortcode)next to each agent so the user can disconnect property and agent on front end?

#1432403

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the link of the content template you are using to display property, once I review that I will be in better position to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1432687

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - thank you for sharing all required information.

I've checked that you are using the following view to display all agents the related posts:
=> hidden link

Basically, there is no such native feature available, but the following is the workaround.

I've added the following link to your view's content template:

<a href="/disconnect-m2m-page?child=[wpv-post-id item='$current_page']&parent=[wpv-post-id]"> Disconnect posts </a>

Created the following page and there I've added the [delete_relationship] shortcode to its post body:
=> hidden link
-The shortcode code is adjusted as given under within the custom code section offered by Toolset:

// Put the code of your snippet below this comment.
function func_delete_m2m_relationship($atts) {
     $parent = $_GET['parent'];
     $child = $_GET['child'];
     if(!is_admin()) {
        toolset_disconnect_posts('properties-agents', $parent, $child );
        $location = $_SERVER['HTTP_REFERER'];
        wp_safe_redirect($location);
        exit();
      }
}
add_shortcode("delete_relationship", "func_delete_m2m_relationship");

Please check the following single property page on different browsers where you are not logged in as admin- click on the "Disconnect posts" link:
hidden link

Can you please confirm the workaround I shared help you to disconnect the posts.