Skip Navigation

[Resolved] Relationship Form without select2 field

This support ticket is created 2 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
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: Africa/Casablanca (GMT+00:00)

This topic contains 11 replies, has 2 voices.

Last updated by Bob 2 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2038247

Bob

I have the following set up Members and Books for a library site

Two custom types and a one to many relationship between them

It's all working fine with the relationship form but the client doesn't like drop down selection field - we will be using a barcode scanner going forward so it isn't really needed.

What I've tried to do is set up a template for the Members and I have a view on the page which provides the selection of the Book - all fine but I end up with the Member post-ID and the Book post-ID in separate views and I can't combine them into a shortcode field to then fire off toolset_connect_posts. I've attempted to get around it with jQuery pulling the IDs from different fields but I'm stuck and think I need to use AJAX (if so could you point me in the right direction) or (preferably) something clever with the template or PHP.

Any and all help appreciated as ever.

#2038989

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

If I understood well, you want to connect a book to a member, and you want to get the book ID using a barcode scanner, right?

If that's the case, I would suggest using a form that does not have a relationship field. Add a generic field(number field) to be used as the book ID. You will have to focus on this field before using the barcode scanner, to get the book ID in the field after the scanner finishes. Then, you can use the cred_save_date hook to use the toolset_connect_posts function.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

If, I did not understand your request, please provide more details.

#2039051

Bob

Thanks Jamal your understanding is correct

Are you suggesting I create a new (effectively redundant) post type and then a post form that is set up to record the book ID? (I have no issue with that but how do I get the Member ID (I need both to create the relationship). I guess I could add the form to a Member template?

#2039117

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I would say, that depends on your website's features and how you want to implement them. I am not sure to understand the Member<>Book relationship and its purpose for your website.

Keep in mind that a Toolset form is always bound to ONE post. Either it creates it or it edits it.

Assuming that you want to put the form in the member content template, I would say, that an edit form for members will be enough. You will get the member ID in the hook's arguments, and also from the forms fields. And you will get the book ID from the form's fields, something like $_POST['slug-of-the-generic-field']

#2039143

Bob

For explanation it's a library so members can borrow multiple books. What I have currently is a template for single members and on that a relationship form allowing me to create the relationship by selecting a book - the problem with this is firstly I can't force focus on this field and I can't directly scan in the code without then selecting the book from the drop down.

Additionally I need to be able to repeat this process as a member can borrow more than one book.

I'm going to try your suggestion and I'll get back to you, if the above throws any light on the subject please come back to me whenever.

#2039183

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Well, I would say that you'll need to rethink the data model. Especially because of the following reasons:
- It can't be a one-to-many relationship between a member and a book, because a member may borrow multiple books, and a book may be borrowed by multiple members. A many-to-many relationship should be used.
- It can't be a many-to-many relationship either. Because a member may borrow the same book multiple times. So, we'll need to have multiple links between a member and a book. A many-to-many relationship will allow us to link a member with a book only one time.

I'll suggest introducing another custom post type that will represent a borrowing action. Let's call it "Borrowing". This post type will have a many-to-many relationship with Books and a one-to-many relationship with members:
- A Borrowing may have multiple books, and a book can be borrowed multiple times.
- A Borrowing is related to only one member, and a member can borrow multiple times.

The form to borrow books will create a post of this Borrowing post type, and you can use custom code to link the books to it and to link the member to it.

You can use generic fields or you can use additional custom fields in this post type:
- A number field to store the Member ID.
- A repeatable number field to store the Books IDs.
This will make the fields available in the form, but, it is somehow redundant(storing the relationship in Toolset and in these custom fields).

#2039201

Bob

Thanks I'll give that some thought.
In fact a book can only be borrowed by one member at a time - they are physical objects. Once the book is returned the relationship is deleted and the book can then be borrowed by a different member or indeed the same member but a new relationship is set up each time - does this alter your opinion?

#2039609

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

If you will remove the relationships when the book is returned, you can use a Many-to-many relationship between Members and Books without needing to use my suggested "Borrowing" custom post type. But, you won't have a history of who booked what? and when? Your decision 🙂

I'll set this ticket as waiting for your feedback to let your test and try and I'll remain at your disposal. Without a reply, the thread will remain open for 2 weeks, and you will receive an email notification before the automatic closure of the ticket.

All the best!

#2039645

Bob
#2039647

Bob
#2039649

Bob

Thanks Jamal - I didn't mention it but not recording the history was part of the brief but thanks for the heads up.

I've implemented your original advice with the generic field and used that within a edit form for the Member and then retrieved the data with $_POST['loan_item'] and then using that a cred_save_data with a toolset_connect_posts.

I'll close the ticket now and thanks for your help.

#2039651

Bob

Thanks Jamal - I didn't mention it but not recording the history was part of the brief but thanks for the heads up.

I've implemented your original advice with the generic field and used that within a edit form for the Member and then retrieved the data with $_POST['loan_item'] and then using that in a cred_save_data with a toolset_connect_posts.

Thanks for your help.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.