I've been stuck a while because in a Cases logic hook I couldn't load contacts_cases relationship as I did with another custom relationship, like this:
$bean->load_relationship('cases_lines');
$lines = $bean->cases_lines->getBeans();
$bean->load_relationship('contacts_cases');
$contacts->$bean->contacts_cases->getBeans();
The first was working, the latter wasn't.
I found out that I was using the wrong name because it's a default relationship and its name is 'contacts' and not 'contacts_cases' as I supposed, so I change my code like this:
$bean->load_relationship('contacts');
$contacts->$bean->contacts->getBeans();
and now it's working perfectly.
I didn't found in documentation the naming difference between default and custom relationship, I found the solution above only debugging SugarBean method 'load_relationship' and printing out the loaded relationships.
This line of code
error_log('LOADED RELATIONSHIPS '.print_r($this->loaded_relationships, true));
printed out
Array\n(\n [0] => cases_lines\n [1] => contacts\n)\n
In Studio the relationship appears as 'contacts_cases', the only place where I found the name 'contacts' is in file modules/Cases/vardefs.php
'contacts' =>
array (
'name' => 'contacts',
'type' => 'link',
'relationship' => 'contacts_cases',
'source'=>'non-db',
'vname'=>'LBL_CONTACTS',
),
Also in relationships table the relationship_name is 'contacts_cases'.
Is there any place where I can see the real names that I must use when calling load_relationship or the only solution is looking in vardefs?
The easiest single source is to look into the cache directory because this merges all custom and out-of-the-box fields and relationships into one big array. To see all relationships from an Account perspective, check cache/modules/Accounts/Accountsvardefs.php
and look for the relationships
key and any fields
where the type
parameter is link
. You'll find "stock" relationships like contacts
and opportunities
as well any and all custom relationships are built into that array.
Note that custom relationships will be called different things depending on the object you're working with. For example, a custom one-to-many relationships between custom modules Meals and Ingredients might be named key_meals_ingredients_meals
on the Meals side and key_meals_ingredients_ingredients
on the Ingredients side.
You can also find these in the user interface using Studio. Navigate into Studio, then your module, then into the Relationships table. The Name column on the left of the Relationships table is the value you're after.