How i can trigger if some changes by relations (m:m or polymorphic by m:m)

I have polymorphic relations, like:

All has many-to-many relation, so I create a polymorphic relation, many to many.
What I want to do:

If by category add new course or delete, sync with company and remove or add a same course.
If I add a new company to a category, a company gets all the courses too.

My last attempt is to trigger js ajax events and then execute my own js ajax.