Creating 1-to-many relationships in Sugar 5.2 with no Join Table

url:http://www.sugarcrm.com/wiki/index.php?title=Creating_1-to-many_relationships_in_Sugar_5.2_with_no_Join_Table

Replace parent and child in the following folder names, file names and code where

parent = One/left hand side of relationship (table/lowercase module name) child = Many/right hand side of relationship (table/lowercase module name) parentmn = parent module name childmn = child module name parentdn = parent dictionary name - see modules/parentmn/vardefs.php for dictionary name childdn = child dictionary name see modules/childmn/vardefs.php for dictionary name parentdl = parent dictionary name in lowercase

Replaced values should follow case variations; e.g. replace childmn with zz_assets and replace CHILDMN with ZZ_ASSETS. Exceptions occur for mixed-case module and dictionary names where the lowercase replacement must follow the name, e.g. Meetings.

Create new file custom/Extension/modules/parentmn/Ext/Layoutdefs/parent_child.php

<?php
$layout_defs['parentmn']['subpanel_setup']['child'] = array (
'order' => 99, // number to set appearance order if many subpanels
'module' => 'childmn',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
'title_key' => 'LBL_CHILD_SUBPANEL_TITLE',
'get_subpanel_data' => 'child', // link field name from parent
);
?>

Create new file custom/Extension/modules/parentmn/Ext/Vardefs/parent_child.php

<?php
$dictionary['parentdn']['relationships']['parent_child'] = array(
'lhs_module' => 'parentmn',
'lhs_table' => 'parent',
'lhs_key' => 'id',
'rhs_module' => 'childmn',
'rhs_table' => 'child',
'rhs_key' => 'parentdl_id',
'relationship_type' => 'one-to-many'
);

$dictionary['parentdn']['fields']['child'] = array (
'name' => 'child',
'type' => 'link',
'relationship' => 'parent_child',
'source'=>'non-db',
'side' => 'right',
);
?>

Create new file custom/Extension/modules/childmn/Ext/Vardefs/parent_child.php

<?php
#create relationship to parent
$dictionary['childdn']['fields']['parentdl_id'] = array (
'name' => 'parentdl_id',
'type' => 'id',
'len' => 36,
);

$dictionary['childdn']['fields']['parentdl_name'] = array (
'source' => 'non-db',
'name' => 'parentdl_name',
'vname' => 'LBL_PARENTDL_NAME',
'type' => 'relate',
'len' => '255',
'id_name' => 'parentdl_id',
'module' => 'parentmn',
'link'=>'parent_link',
'join_name'=>'parent',
'rname' => 'name',
#uncomment following to create field when parentdl_name a combined field

/*
'table' => 'parent',
'db_concat_fields' =>
array (
0 => 'first_name',
1 => 'last_name',
),
*/
);

$dictionary['childdn']['fields']['parent_link'] = array (
'name' => 'parent_link',
'type' => 'link',
'relationship' => 'parent_child',
'link_type'=>'one',
'side'=>'right',
'source'=>'non-db',
);

#create index
$dictionary['childdn']['indices'] = array(
array(
'name' =>'parentdl_id',
'type' =>'index',
'fields'=>array('parentdl_id')
),
);
?>