Skip to content

Instantly share code, notes, and snippets.

@pgorod
Forked from jmertic/gist:5660495
Last active March 12, 2020 14:20
Show Gist options
  • Save pgorod/bd53339fbf6bddbed824c762539ed058 to your computer and use it in GitHub Desktop.
Save pgorod/bd53339fbf6bddbed824c762539ed058 to your computer and use it in GitHub Desktop.
Programmatically Find the Name of the Relationship between two Modules
<?php
function getRelationshipByModules ($m1, $m2)
{
global $db,$dictionary,$beanList;
$rel = new Relationship;
if($rel_info = $rel->retrieve_by_sides($m1, $m2, $db)){
$bean = BeanFactory::getBean($m1);
$rel_name = $rel_info['relationship_name'];
foreach($bean->field_defs as $field=>$def){
if(isset($def['relationship']) && $def['relationship']==$rel_name) {
return(array($def['name'], $m1));
}
}
} elseif($rel_info = $rel->retrieve_by_sides($m2, $m1, $db)){
$bean = BeanFactory::getBean($m2);
$rel_name = $rel_info['relationship_name'];
foreach($bean->field_defs as $field=>$def){
if(isset($def['relationship']) && $def['relationship']==$rel_name) {
return(array($def['name'], $m2));
}
}
}
return(FALSE);
}
@pgorod
Copy link
Author

pgorod commented Mar 12, 2020

Programmatically Find the Name of the Relationship between two Modules
Sugar Dev Team — May 29, 2013 — 2 Comments

Editor’s Note: This post came from Sugar Community member Francesca Shiekh. Thanks Francesca!

I often find myself referencing Jeff Bickart’s “HOWTO: Using the bean instead of SQL all the time.” (http://developers.sugarcrm.com/wordpress/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/ )

But when it comes to using the tips in “Adding and removing related records” finding the right relationship name is always a challenge (it is NOT the relationship_name field in the relationship table!).

Also, I sometimes need to find that relationship name programmatically based on module names.

To address this problem I created the following function which, given two module names, returns the name of the relationship needed to load, add and delete relationships between records. If there is no relationship between the modules the function returns FALSE.

Taken from https://web.archive.org/web/20160324015316/http://developer.sugarcrm.com/2013/05/29/programmatically-find-the-name-of-the-relationship-between-two-modules/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment