Product Templates Many-to-Many Relationship

sugarcrmdevelopers —  June 26, 2012

SugarCRM Administrators may notice when mucking about with the Product Templates module in Studio that the relationships are constrained. When adding a new relationship, we aren’t given the ability to add a many-to-many relationship. What’s so special about Product Templates that we can’t do this?

I had to dig through the code for the answer, but it was pretty obvious once I found it. When generating this dropdown list of relationship-type options (referred to as the $cardinality array), Studio simply checks to make sure that both sides of the relationship actually have subpanels. Product Templates, by default, doesn’t have a subpanel in the system and thus a Many-to-Many relationship wouldn’t work very well.

For the curious, check ViewRelationship::display(), in modules/ModuleBuilder/views/view.relationship.php lines 110-200 to see how $cardinality is build, checked, and assigned to the Smarty template.

So our solution? I threw a basic default subpanel into the custom directory at /custom/modules/ProductTemplates/metadata/subpanels/default.php. It was largely a copy of the existing default subpanel for Products, with a few lines changed and commented-out for simplicity. Dropping this into the system, then reloading the Relationship page, and I found that the Many-to-Many option had become available. From this point, I created my relationship and the client is able to customize the subpanel like any other via Studio.

I packaged my default subpanel for later use, you can download it: Product Templates Default Subpanel.