This tutorial is for those who:
* want to create one-to-many relationship between two modules without an intermediate table (like ModuleBuilder does)
* has a relate field in a module and want to display a subpanel to the related module
* what to make this using code, in an upgrade safe manner

If you are using SugarCRM and you want to create one-to-many relationship between two modules via ModuleBuilder an aditional table will be created even if that isn’t necesary(no need for a table now for this type of relationship).
Eliminating additional table will improove the application performance and will make more easy to maintain.

Let’s consider the following scenario:
We have two modules: Invoices and Accounts. In custom Invoice module we already have a related field, billing_account_id that makes connection with Account module. We want to display in a subpanel from Accounts module all the invoices that are related.

If you have a related field in Invoices does’t mean that you have the relationship too. So we’ll have to create 4 files for that:
* /custom/Extension/modules/inv_Invoices/Ext/Vardefs/inv_invoices_accounts.php – where have to add the relationship itself and a link field between Invoices and Accounts
* /custom/Extension/modules/Accounts/Ext/Vardefs/inv_invoices_accounts.php – where have to add a link fild between the two modules
* /custom/Extension/modules/Accounts/Ext/Layoutdefs/inv_invoices_accounts.php – definition for invoices subpannel
* /custom/Extension/modules/inv_Invoices/Ext/Language/en_us.inv_invoices_accounts.php – a language variable with the name of the subpanel

Let’s see the code:

* /custom/Extension/modules/inv_Invoices/Ext/Vardefs/inv_invoices_accounts.php
$dictionary['inv_Invoices']['fields']['accounts'] = array(
'name' => 'accounts',
'type' => 'link',
'relationship' => 'inv_invoices_accounts',
'module' => 'Accounts',
'bean_name' => 'Account',
'source' => 'non-db',
'vname' => 'LBL_ACCOUNTS',
);

$dictionary['inv_Invoices']['relationships']['inv_invoices_accounts'] = array(
'lhs_module' => 'Accounts',
'lhs_table' => 'accounts',
'lhs_key' => 'id',
'rhs_module' => 'inv_Invoices',
'rhs_table' => 'inv_invoices',
'rhs_key' => 'billing_account_id',
'relationship_type' => 'one-to-many',
);

* /custom/Extension/modules/Accounts/Ext/Vardefs/inv_invoices_accounts.php
$dictionary['Account']['fields']['inv_invoices'] = array(
'name' => 'inv_invoices',
'type' => 'link',
'relationship' => 'inv_invoices_accounts',
'module' => 'inv_Invoices',
'bean_name' => 'inv_Invoices',
'source' => 'non-db',
'vname' => 'LBL_INVOICES',
);

* /custom/Extension/modules/inv_Invoices/Ext/Language/en_us.inv_invoices_accounts.php
$mod_strings['LBL_INVOICES_SUBPANEL_TITLE'] = 'Invoices';

Atention, we supose that we already have the definition for the related field in Invoice module vardefs.php :
$dictionary['inv_Invoices']['fields'] = array (
'billing_account_name'=>
array(
'name'=>'billing_account_name',
'rname'=>'name',
'group'=>'billing_address',
'id_name'=>'billing_account_id',
'vname'=>'LBL_BILLING_ACCOUNT_NAME',
'type'=>'relate',
'link'=>'billing_accounts',
'table'=>'billing_accounts',
'isnull'=>'true',
'module'=>'Accounts',
'importable' => 'required',
'required'=>true,

),
'billing_account_id' =>
array(
'name'=>'billing_account_id',
'type'=>'id',
'group'=>'billing_address',
'vname'=>'LBL_BILLING_ACCOUNT_ID',
'table' => 'accounts',
'isnull' => 'true',
'module' => 'Accounts',
'dbType' => 'id',
'reportable' => false,
'massupdate' => false,
'duplicate_merge' => 'disabled',
),
);

At the end, you’ll have to make a “quick repair and rebuid” and in Acounts modules you’ll have to see the Invoices subpanel(if the curent account was assingned to an invoice).

That’a all folks :)