Skip Navigation

[Resolved] How to automatically select both sides of a many to many relationship in a form

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to automatically select both sides of a many to many relationship in a form

Solution:
There is no such feature exists to set both parent and child using post relationship form.

https://toolset.com/forums/topic/how-to-automatically-select-both-sides-of-a-many-to-many-relationship-in-a-form/#post-954437

Relevant Documentation:

This support ticket is created 6 years, 4 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)

This topic contains 17 replies, has 4 voices.

Last updated by Ahmed 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#922754
screenshot1.PNG

I have a CPT called Doctors and another one called patients. Both these CPT are in a many-to-many relationship. I also have a view which a doctor can use to search for patients. What I am trying to do is to let this doctor to be able to connect to those already-existing patients as children by pressing the add patient link/button (see screenshot of a test page, or see the live example hidden link). There is an intermediary form but I don't want the doctor to fill it at this point just yet. I just need doctors to be able to connect patients to their CPT at a button press without needing to go to another page with a relationship form and again select the patient.

Now in one-to-many relationships, I can do this by actually creating a CRED form that automatically selected the current user (doctor) as the parent and then I can hide this parent field and then rename the submit button to "Add Patient" and let only the submit button of the CRED form appear. I can then add that CRED button to the view mentioned above so that it appears in the same row as every patient, and that way I can let the doctor add any of the patients CPT in the list as children without having to leave the page or fill any form.

Now in many to many relationships that same approach doesn't seem to work with relationship forms as I can't simply add a parent selection field and then let it select the current user automatically. I need the relationship form to inherit the patient from the view and get the parent (doctor) from the current user so that I can hide those fields and only show the submit button (add patient button).

What is the best approach to achieve this in many-to-many relationships?

#922906

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I checked with the relaitonship forms shortcode that displays the parent and child select2 field that will allow you to connect the posts but unfortunately there is no such feature available to preselect the parent and child posts with respective select2 fields.

If you agree - I can take this as a feature request.

#924444

Yes please. I think that this is a very important feature request and I think it should be there by default just like post forms.
Do you have any workarounds for me? I just want the user(doctor) to search for patients by their phone number, then he gets the name of the patient (just to be sure it is really the patient he wants) and then he can add him/her (connect to it). Can you suggest anyway for me to do this or something similar for now?

#924599

Minesh
Supporter

Languages: English (English )

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

Ok - thank you. I'll pass your concern to our team who is managing the feature requests for assessment.

Please note that there is no ETA on it.

#948853

Any suggestion of a different way to do it until the feature is added? As I said, I just need to search for patients by phone numbers and then be able to connect to any of the resulting patients.

#949039

Hello,

You may consider to setup custom PHP codes, connect the "Doctors" and "patients":
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

For example, setup a link to pass the "Doctors" ID and "patients" ID to a wordpress page, in that page use above function to setup the post relationship

#949227

I was not aware that this is possible through PHP. I am not a PHP developer but I will try to work my way around. I think I need to put this php code in functions.php. right?
You gave me hope that this problem may be solved. Thank you.

Kindly leave the thread open as I may need a little nudge in the right direction. I'll try this solution tonight and give you feedback.

#950006

Yes, you can put your custom PHP codes into your theme file "functions.php", please let me know if you still need assistance for it.

#951646

I am sorry for the delay. I was trying to fix my problem based on your suggestion and this is what I came up with (please bear in mind that I don't have any background in PHP so pardon my funny code):

I thought of adding a shortcode in php that connects both doctors and patients through the api you provided (I called it [wpv-connect-docpatient]). I will place this shortcode in my view that displays the patient list (in screenshot above) so that when the user presses 'Add patient' this runs the shortcode (Q1 is that possible, can a button run shortcode?)

My main problem now is in the php code .. it should be something like this:

//creates a shortcode to connect doctor (as parent) to patient
add_shortcode('wpv-connect-docpatient', 'connectdocpt_shortcode');
Function connectdocpt_shortcode($post_id) {
toolset_connect_posts( 'doctor-patient', $parent_id, $post_id);
}

The problem is: I know how to get doctor and patient ids in toolset but don't know hot to get them in PHP:
Q2 In toolset the patient id is displayed as the current post id by the view loop I am using in screenshot. In PHP I guess I get it by $post_id?

Q3 In toolset I get doctor id by a view [wpv-view name="get-doctor-from-current-user"] that returns the raw clean id of doctor correctly (current doctor is the post type with current user as author). How to get it in PHP?

Thank you!

#952005

Since it is a custom PHP codes problem, please provide a test site with the same problem, fill below "private detail box" with login details and FTP access, also point out the problem page URL, how and where do you want to add the button, where I can edit your custom PHP codes, I need a live website to test and debug your codes, thanks

#953702

Minesh
Supporter

Languages: English (English )

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

Hello Ahmed,

Well - as I updated you that we have take this as a new feature request. There is no such feature to connect it directly until you have some custom programming using AJAX and that is beyond the scope of our support policy.

If you need custom programming for your project, please feel free to contact our certified partners:
=> https://toolset.com/contractors/

#953741

Dear Minesh,

I really do not understand your response!! I am not asking for a strange feature out of the scope of toolset like a booking system or calculations etc as some customers do (and they got help even for these out-of-the-scope things by the way)... I am only asking for a very simple thing at the core of toolset: to connect two CPT in a many to many relationship through automatically adding the parent and child. It is something that one-to-many relationships can already do for ages and it should naturally be available in many to many as well because it just doesn't make sense not to be able to do it.

Now that I also know that you have a toolset command that can help me(toolset_connect_posts), and you say it is out of scope?! It is a TOOLSET command. How can that be out of scope?! Denying me help with a toolset command as a workaround for a toolset problem is really upsetting and unacceptable !!!

I want an example code to work with to know how to use this command to connect two posts!! This is the least you can do and I don't think that this is too much to ask!

#954437

Hello Ahmed,

I'm Mohamed, the Toolset support team leader.

I've gone through this ticket replies and I see that you are right. this should be implemented.

I've already tried to implement a workaround specifically for you but unfortunately it's not possible.
The problem is that there is no hook available that works after submitting the relationship form.

We neither have an API for the relationships forms. The good news is that we have an open ticket in our internal queues for that to be implemented.

The reason why we will implement this is that we already have the CRED API for post forms: https://toolset.com/documentation/programmer-reference/cred-api/ and we need a similar one for relationship forms to make our clients able to control the relationships forms in the way they wish.

I can't promise you when this can be implemented but I will try to push forward implementing this as soon as possible.

Thanks.

#954477

Dear Mohammed,

Thank you very much for your response and for trying to come up with a workaround for me. I really appreciate it.
I would add that we also need to be able to use a shortcode to pass an id as the parent/child field value in a relationship form.

For example, in a post form we cab do this:


  [cred_field field='@provider-service-order.parent' select_text='--- not set ---' value='[wpv-post-id]' class='form-control' output='bootstrap']

If this is available for relationship forms, it would solve my problem as I would be able to automatically select parent/child without the need of any hooks or PHP. This was actually my original approach to solve this problem but it didn't work in relationship forms.

I really hope that you add that as a feature request as well, to make relationship forms as versatile as post forms.

Again, thank you very much for your response and care.

#954480

Can I leave this thread open to be informed if any if these features became available? or should I close it?