How to get a list of relations from SugarBean module?

Hi everyone, trying to get a list of module relationships by name, so I can use parameters for a direct query?

$link =new Link2('link_name',$sugarBean);
$link->relationship->def['relationships']['link_name'];

but I don’t think this is a very correct way

This is an interesting question.

Does this advice work? I never tried it, just googled it a minute ago:

Thenk you.

This will work for relationships that are in a vardef.php, but does not work for relationships that are in custom / metadata.

All relationships I get so:

$link =new Link2('link_name',$sugarBean);
$relationship_params=$link->relationship->def['relationships']['link_name'];
var_export($relationship_params);
array(
                    'lhs_module' => 'HRPAC_VACANCY',
                    'lhs_table' => 'hrpac_vacancy',
                    'lhs_key' => 'id',
                    'rhs_module' => 'Users',
                    'rhs_table' => 'users',
                    'rhs_key' => 'id',
                    'relationship_type' => 'many-to-many',
                    'join_table' => 'user_ram',
                    'join_key_lhs' => 'parent_id',
                    'join_key_rhs' => 'user_id_c',
                    'relationship_role_column' => 'parent_type',
                    'relationship_role_column_value' => 'additional_managers',
                )

Bat order for it to work, I had to change it in file data/Link2.php
replace
protected $relationship;
with
public $relationship;

1 Like

What about this solution, would it work for all cases?

        $rels = [];
        foreach ($bean->getFieldDefinitions() as $key=>$val) {
            if (isset($val['type']) && $val['type'] === 'link') {
                $rels[] = $key;
            }
        }

This starts with a $bean (for example, inside a logic hook) and produces a rels[] array with all valid relationship names.

I think this uses the same set of definitions that the `load_relationships uses, so it should be accurate.

@palach
I want to enter to your discussion.

I am sure that the code from example works for relationships in metadata and vardefs.

$bean->load_relationship('cases_lines');
$lines = $bean->cases_lines->getBeans();

If you want to get list of relationships for bean you can call:

$listLinks=$bean->get_linked_fields();

You’re right, I didn’t know about this, but it is basically equivalent to my code, except it returns the full array elements instead of just the string names.

I will get this params

'lhs_module' => 'HRPAC_VACANCY',
                    'lhs_table' => 'hrpac_vacancy',
                    'lhs_key' => 'id',
                    'rhs_module' => 'Users',
                    'rhs_table' => 'users',
                    'rhs_key' => 'id',
                    'relationship_type' => 'many-to-many',
                    'join_table' => 'user_ram',
                    'join_key_lhs' => 'parent_id',
                    'join_key_rhs' => 'user_id_c',
                    'relationship_role_column' => 'parent_type',
                    'relationship_role_column_value' => 'additional_managers',

@palach
If you want to get this array you can write the code below. It return array with list of all relationships. You should know the name of interesting for you relationship for get it .

require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php';
$relationships = new DeployedRelationships(<name_of_Module>);
$relationshipsList=$relationships->getRelationshipList();