Associate records in plugin

When creating N:N relationships between entities, CRM allow us to associate records. This is a great functionality to track and reference records through entities. Although this can be done manually, find below a method to associate two records programmatically: