-
-
Save pgorod/bd53339fbf6bddbed824c762539ed058 to your computer and use it in GitHub Desktop.
Programmatically Find the Name of the Relationship between two Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/