Skip to content

How to add a new Many to Many relationship

Matt Marum edited this page Mar 10, 2016 · 1 revision

Limitation

  • Many-to-many relation field data can be generated only for modules that are also generated by Tidbit
  • Relationship should exists in Sugar instance

Description

Using Tidbit you can create Many-to-Many relationships between modules that are also generated by Tidbit

How to

Many-to-many relationships in Sugar usually require separate tables that will contain relationship information like relate bean ID and sometimes other additional relationship data. To define new Many-to-many relationship in Tidbit, you need to add entry to $tidbit_relationships array with relation description, so that Tidbit will know how to populate them. A New relationship definition contains 3 mandatory fields and some optional fields:

  • "self" - field name in DB table that would contain ID of current module bean
  • "you" - field name in DB table that would contain ID of related module bean
  • "table" - table name, where relationship information is stored in DB
  • "ratio" - (optional) number of relationships per one generated bean. Default is 1
  • "random_id" - (optional, true/false) - if this option is true, random ID from generated interval will be used, instead of linear generation
  • "random_ratio" - (optional, should has "min" and "max" entries) - generate random number of relations in range of (min, max). Usually used with "random_id"

To create a new relationship definition in Tidbit between custom module "MyTests" and "Accounts" you need add php code like this

// Assume that MyTests module created via Studio and package name is "test"
$tidbit_relationships['test_MyTests'] = array(
    'EmailAddresses' => array(
        'you' => 'test_mytests_accounts_test_mytest_ia',
        'self' => 'test_mytests_accounts_accounts_idb',
        'table' => 'test_mytests_accounts_c',
    )
);

To learn how to add Custom modules to Tidbit, see this article. Below is an example.

Examples:

Definition of Opportunities Relations with Quotes and Tags

$tidbit_relationships['Opportunities'] = array(
    'Quotes' => array(
        'self' => 'opportunity_id',
        'you' => 'quote_id',
        'table' => 'quotes_opportunities'
    ),
    'Tags' => array(
        'self' => 'bean_id',
        'you' => 'tag_id',
        'table' => 'tag_bean_rel',
        'random_ratio' => array('min' => 0, 'max' => 3), // Define random Tags number per record
        'random_id' => true,
    ),
);

Definition of Users Relationships

$tidbit_relationships['Users'] = array(
    'Teams' => array(
        'self' => 'user_id',
        'you' => 'team_id',
        'table' => 'team_memberships'
    ),
    'Calls' => array(
        'self' => 'user_id',
        'you' => 'call_id',
        'table' => 'calls_users'
    ),
    'Contacts' => array(
        'self' => 'user_id',
        'you' => 'contact_id',
        'table' => 'contacts_users'
    ),
    'Meetings' => array(
        'self' => 'user_id',
        'you' => 'meeting_id',
        'table' => 'meetings_users'
    ),
);

Clone this wiki locally