Does someone has a link to some tutorial about the Relationship Field?
The docs somehow vaguely explain what they do but I would like to see some real examples on how this is used.
https://docs.expressionengine.com/latest/fieldtypes/relationships.html
More about how it actually related entries to one and another. Could I do the following for example?
Have an Orders channels. And invoice channels.
I put entries in orders, and other entries into invoices.
Then 1 Order entry has multiple related invoices that only related to that specific order but others.
When I pull that order, be able to only match the related invoices (but not others).
Or when I pull the invoice entry see the order it belongs.
Would this be a case for this field or maybe I’m better of just making my own text field and putting the ID for the order or invoices in the entries and matching them manually? I mean, this would be simple, just another field in bother orders and invoices I can check but maybe this field already does this automatically.
I believe Andy is putting together a more in-dpeth response as a whole. However I did just want to share the following video tutorial on relationships.
Thanks,
-Tom Jaeger
Wow, that was fast 😁😁😁. Thank you, Tom. I will check the tutorial in a few minutes.
Thanks for the drawing Andy, my question was if the field works more or less like a Foreign Key in a SQL database that links the same data together in a different table. In your drawing I see you specified an order_id and invoice_id. That is actually my main question.
I understand correctly from your drawing, the order_id would hold the same value automatically as the invoice_id using that field which would save me from having to query all the channels with a specific ID first?
vw000. As you pointed out if I was laying out just the DB tables for this, then I would have order_id as a foreign key in the Invoices table. That’s not exactly how EE actually stores the data though. I was just trying to help visualize it with the drawing (mixed EE fields with ERM symbols 😊 ).
In reality, The relationships are stored in exp_relationships
. Each relationship is saved with:
* parent_id
- this would be your order entry’s ID.
* child_id
- this would be your invoice entry’s ID.
* field_id
- this is the ID of the relationship field you are using and how EE knows to grab the data or not
* order
- this tells EE what order you have the relationships in the entry you related them to.
Each time an entry is related to another entry, another row is added to exp_relationships
. Attached is an example where I have related 3 entries to the same entry.
Hope that makes sense.
I see, but let me get this correctly. After watching the video, he selects the related entries from the control panel. Is that correct? You actually need to manually select each relationship? If that is the case then is more like showing related content and would not really work for my case (orders-invoices).
The reason is that I will not go into the control panel and manually select which invoices belong to which order. I had the impression this field will relate the entries automatically.
Let me explain.
I create a text field called customer_id. I assign this to the both Order and Invoices channels and make it as required.
Now when I submit a new Entry Order I put the customer ID, for example “750”.
I then go and submit 3 or 5 new Entry Invoices, and use the same 750 number on the same field.
Can Expression Engine now using the tags automatically get all the invoices for that specific order from that field alone? Or do I actually have to specify the relationship from the control panel like in that video?
It’s also not clear while creating that field if I should select both channels or just the one, and which one should hold that field. Since in my case I only want the orders to relate to multiple invoices, but one invoice should not be able to relate to multiple orders, but just one.
I understand if my question is confusing, but I’m even more confused because in the video and from my own tests you actually have to manually specify the relationship while creating the entry.
I guess my question in a more simple way Andy is…Looking at your draw screenshot.
Do you have in that particular example go to Bobs Order channel entry #456 and set the invoices 56, 57 and 58 to be related or will your example still work without doing anything in the control panel? Just using the data inside the entries?
Ok. That what you’re describing does change things. Trying to break this down some…
After watching the video, he selects the related entries from the control panel. Is that correct? You actually need to manually select each relationship?
Yes, to use the relationship field you manually related entries in the Control Panel
It’s also not clear while creating that field if I should select both channels or just the one, and which one should hold that field. Since in my case I only want the orders to relate to multiple invoices, but one invoice should not be able to relate to multiple orders, but just one.
You only select the channel you want to be able to choose entries from. Continuing our example so far, the related_invoices
field is going to be a list of entries from the Invoices channel. Thus you would select the Invoices channel when creating the field. The related_invoices
field would then be added to the Orders channel. This would allow me to create an Order entry and select what Invoices are related to this Order when I’m creating a new Order entry.
Now, I understand that the above isn’t really what you actually want to do. Few options:
Use relationship fields with channel forms. This would allow you to do this with native fields. However, I think you would actually flip the relationship to make this work. Your Invoice would be the parent and the Order would be the child (I know it’s opposite how you might normally set this up). So you would create a relationship field in your Invoice channel that would list available orders. Then when you display an Order, you would actually use the parents
tag to display all Invoices that are parents to this Order. Again, it’s backwards but allows you to use current native features.
A little more hacky, is to simply have a text field that you enter the Order Id you want to attach an invoice to. Then when you display an Order, you use the exp:query
add-on to query for all entries that have the current Order as the related Order Id.
Same as #2, except you but this in a custom add-on and just create a template tag where you pass the current Order Id and your add-on returns the list of Invoice Ids that you can then pass into a standard {exp:channel:entries}
tag.
Thank you for the information. I think I will use relationships for something I asked in the past, matching specific help articles and having a suggestion of similar articles to users, I was not sure how to do that in the past and did with categories (not great). For that specific use, matching them in the control panel is not only fine but actually what I want to be able to suggest the proper related content.
But for my usage case here, this will probably not work. I will not match relationships from the control panel which seems like extra work.
It’s rather easier just to have an ORDER_ID field on the Invoice channel, then when I submit Invoice entries I just put that unique identifier from the order and can relate invoices to orders using simple EE parameters which might sound way more simple.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.