Programmers and developers are wondering: "How to connect modules in Vtiger CRM among themselves?". At the same time, the most common connection between modules is one to many.
This connection means that in the card of the parent (main) module there is a field in which you can select the desired record of the subsidiary module. And in the card of the subsidiary module there is a tab, when you click on it you can see all the records associated with this card.
Thus, in order to properly organize a one-to-many relationship in Vtiger CRM, we need to do the following:
- in the first module, we create a field that connects us to the record of the child module;
- in the second module, we create a linked list that will allow us to view the records that were selected through this field.
In other words, we need two functions. They can be used in work, for example, when you write custom functions or scripts to create a module. Their use will allow you to quickly organize a one-to-many relationship between two modules.
public function createRelationship($module1, $module2, $fieldLabel)
{
include_once "vtlib/Vtiger/Module.php";
$block = 'LBL_CUSTOM_INFORMATION';
$module = Vtiger_Module::getInstance($module1);
$module1Class = Vtiger_Module::getClassInstance($module1);
$block1 = Vtiger_Block::getInstance($block, $module);
$columnName = "";
if ($actionRelatedList == true) {
$columnName = "cf_nrl_" . strtolower($module2) . rand(1, 1000) . "_id";
} else {
$columnName = "cf_" . strtolower($module2) . "_id";
}
$fieldName = $columnName;
$field1 = new Vtiger_Field();
$field1->label = $fieldLabel;
$field1->name = $fieldName;
$field1->table = $module1Class->table_name;
$field1->column = $fieldName;
$field1->generatedtype = 2;
$field1->columntype = "INT(10)";
$field1->uitype = 10;
$field1->typeofdata = "I~O";
$field1->quickcreate = 2;
$field1->sequence = 7;
$block1->addField($field1);
$field1->setRelatedModules(array($module2));
$block1->save($module);
return $field1->id;
}
The createRelationship function allows us to create in the first module (module1) a field associated with the second module (module2), which will allow us to select the necessary records from the second module and associate them with the first. The function will create a field in the format cf_potentials_id, where potentials is the name of the associated module. The field will be created in the LBL_CUSTOM_INFORMATION block.
The function accepts the following parameters:
- module1 - the main module in which the field with the associated module will be added,
- module2 - the module with which we plan to communicate through this field,
- fieldLabel - as we plan to name our new field. It is advisable to name the fields in English and then translate it through translation files.
After we made communication with the second module in our main module, now in the second module we should see all the records that are currently attached to it. To do this, we need to display the linked lists tab, when clicked, the related records we need are displayed. You can do this through another function:
/**
* @param $module1
* @param $module2
* @param $relListLabel
*/
public function addRelatedList($module1, $module2, $relListLabel, $actions)
{
include_once "vtlib/Vtiger/Module.php";
$module = Vtiger_Module::getInstance($module2);
$module->setRelatedList(Vtiger_Module::getInstance($module1), $relListLabel, $actions, "get_dependents_list");
}
The function accepts the following parameters:
- module1 - the module in which we plan to display a linked list,
- module2 - the module in which we previously created the field and which will display related records,
- relListLabel - the heading of the list, usually it matches module2,
- actions - accepts an array of actions that can be done in the list. For example: array ('add', 'select') - this array will allow you to add new and select existing entries in the linked list.
Attention! Use these functions at your own risk. And do not forget to backup data before using it. The changes are irreversible.
If you still have questions, I will be happy to answer them through the feedback form.