Skip to content

How to add a new One to Many relationship

Igor Zhutaiev edited this page Jul 17, 2016 · 3 revisions

Limitation:

  • One-to-many relation field data could be generated only for modules that are also generated by Tidbit
  • Relationship should exist in Sugar instance

Description:

Using Tidbit you can populate custom fields or missing module related fields with relate ID data, so you will have generated data linked with existing Sugar data

How to:

One-to-Many relationships in Sugar are based on fields inside relate bean tables. For example, Contacts module is related to Accounts by field "account_id" in "contacts" table. To generate new relationship using Tidbit, you need to update ./config/data/{module_name}.php file inside Tidbit and add new definition for relate field. F.e. you have module called "MyTests" data generated by Tidbit, but missing relation with module Users and this relation is represented by field called "selected_user_id". You need to modify ./config/data/MyTests.php to add relation definition like this

$GLOBALS['dataTool']['MyTests']['selected_user_id'] = array('related' => array('module' => 'Users'));

Also, you can use custom/config.php to load additional configs or override existing settings

After you run Tidbit again, field "selected_user_id" will be generated with values from Users module (that were generated by Tidbit). To learn how to add new/custom module to Tidbit, please see this article.

One-to-many relationship data generation is based on linear formula for both modules and depends on number of records that are going to be generated by Tidbit. F.e. you have 4000 Notes and 1000 Accounts, and have account_id field inside "notes" table that represents Notes → Accounts relation. Using linear formula, first 4 Notes will be associated with first Account, second 4 Notes - with 2nd Account and so on, where 4 was calculated by dividing 4000 Notes by 1000 Accounts. Please check more examples below

Examples:

Contacts have related Accounts with field account_id

// @file ./config/data/Contacts.php
$GLOBALS['dataTool']['Contacts']['account_id'] = array('related' => array('module' => 'Accounts'));

Notes have parent_type and parent_id fields, so we link existing Account for each Note. And each also has relation to one Contact

// @file ./config/data/Notes.php
$GLOBALS['dataTool']['Notes']['contact_id']  = array('related' => array('module' => 'Contacts'));
$GLOBALS['dataTool']['Notes']['parent_id']   = array('related' => array('module' => 'Accounts'));
$GLOBALS['dataTool']['Notes']['parent_type'] = array('value' => "'Accounts'");