This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I'm trying to create a three-way relationship (Googling turned up some interesting results..), but can't quite figure it out.
What I have are three models; Application
, Product
, and ProductMode
.
- A
Product
belongsToManyProductModes
(I have this relationship working). - A
ProductMode
belongsToManyProducts
(I have this relationship working). - An
Application
belongsToManyProducts
and one (Or more) of theProduct
'sProductMode
s. This is what I'm stuck on.
When creating an Application
, you need to be able to choose a series of Product
s and a ProductMode
to link it to.
An Application
could link to the same Product
multiple times, but with a different ProductMode
.
An Application
could link to the same ProductMode
multiple times, but with a different Product
.
In the three-way relationship, the Application
and Product
are mandatory, but the ProductMode
is optional, as not all Product
s have a ProductMode
.
I currently have a DB schema like so:
For the base tables:
applications
| id | name |
|----|----------------|
| 1 | Application 01 |
| 2 | Application 02 |
products
| id | name |
|----|------------|
| 1 | Product 01 |
| 2 | Product 02 |
product_modes
| id | name |
|----|---------|
| 1 | Mode 01 |
| 2 | Mode 02 |
For the pivot table (All three columns are the Primary key):
| application_id | product_id | product_mode_id |
|----------------|------------|-----------------|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 1 |
| 2 | 1 | 2 |
The problem is that I can'y figure out how to get the relationship working between three models, especially when it comes to populating the pivot table.
I did try creating the relationship between only the Application
and Product
, and having the ProductMode
's ID as just pivot data, but then I had issues with the model selector (In the backend form) not showing a Product
that I already chosen, if I wanted to add it again for a different ProductMode
.
All I'm actually trying to achieve is, on the frontend, to have a set of hyperlinks from an Application
to a page to view information on the Product
and the ProductMode
. And vice-versa, if you're on a page for a Product
and ProductMode
, that has linked Application
s, it displays a series of links back to the Application
.
Any advice would be greatly appreciated.
Last updated
Hi, When you need to have complicated relations, you should create additional models for pivot tables and use them to create relations.
In your case, I would create model ApplicationProductMode
for pivot table (and add id
column as primary key in pivot table).
So, relations would be:
Application
hasManyApplicationProductMode
.ApplicationProductMode
belongsToProduct
and belongsToProductMode
.
So, you can get ApplicationProductMode's
of Application and then get Product
data (name) and Mode
data (name) from relations between ApplicationProductMode
and Product
and ProductMode
.
Last updated
Hi Eugene.
Thanks very much! I tried creating a Pivot model, but couldn't quite figure it out, but your explanation makes it much clearer. I've implemented it and it seems to be working perfectly.
1-3 of 3