Skip to content

Database Table Relationship and How To Save Data

I really need you guys’ help.
I just can’t get my head around how magento saves relational data.

For instance, the catalogue-rule module has three tables,

“catalogrule”,
“customer_group”,
“catalogrule_customer_group”.

“catalogrule_customer_group” table saves relationship between “catalogrule” and “customer_group” like so:

enter image description here

I created similar tables with foreign keys but cannot figure out how to set relationship between the tables…

The custom module I am building has “event” and “schedule”(one-to-many) with three tables:
“event”,
“schedule”,
“event_schedule_relationship” just like the catalog-rule.

When I save a schedule item to a schedule table, how can I have magento to save the relationship data(event_id and schedule_id pair) to the “event_schedule_relationship” table?

Also, how do I filter schedules by event_id using the “event_schedule_relationship” table?

I tried to figure it out by looking at the catalog-rule module but I just could not understand how it works.

Could someone please show me a simple example of how I can achieve this?🙏

Thank you!