Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

invaders's avatar

HasManyThrough relation implementation help need

Dear All,

I have 3 tables: Customers id

Relation_Types (can be Supplier or Customer) id

Customer_Relations id customers_id (FK to Customers table) partners_id (FK to Customers table) relation_types_id (FK Relation_Types table)

So it means that a customer A can have many customers through Customer_Relations table and the same customer can have many suppliers through Customer_Relations table, and its customers and suppliers are also stored in Customers table

I would like to get all customers of a customer, and all suppliers of a customer, using relations and scopes.

But also, as a user belonging to a customer, I would like to get all my customers and all my suppliers.

Can you please help me how to implement those relations and scopes ?

Many thanks in advance for your help.

David

0 likes
1 reply
YeZawHein's avatar

I think you won't need 3 tables.

Users

id
name
---
---
role_id (may be supplier and customer)
parent_id ( may be this user(customer or supplier)  has parent customer id and default 0 )

Roles (O-M relation with users)

id
name (can be supplier and customer)

Please or to participate in this conversation.