To unset or remove a 1:M (One-to-Many) relationship between custom modules (or a custom module and a standard module like Calendar/Activities) in Vtiger 7, you’ll need to use PHP scripting with Vtlib functions. Vtlib provides a set of functions to manage module relationships dynamically, including adding or removing relationships.
Step-by-Step Guide to Remove a 1:M Relationship Between Modules in Vtiger CRM 7
1. Understanding the Relationship in Vtiger CRM
In Vtiger CRM, relationships between modules are stored in the vtiger_relatedlists
table. To remove a 1:M relationship, you need to delete the specific entry that links the two modules.
- Parent Module: The module that has a 1:M relationship with another module.
- Child Module: The module that is related as the “many” side in the relationship.
When dealing with the Calendar (Activities) module, which is a standard module, you must be careful to only remove custom relationships to avoid breaking any core functionality.
2. Write the PHP Script to Remove the 1:M Relationship
Below is a PHP script that uses Vtlib functions to remove a 1:M relationship between two modules in Vtiger CRM:
<?php
require_once('vtlib/Vtiger/Module.php');
function removeCustomRelationship($parentModuleName, $childModuleName) {
// Get the parent module object
$parentModule = Vtiger_Module::getInstance($parentModuleName);
if (!$parentModule) {
echo "Parent module '$parentModuleName' does not exist.";
return;
}
// Get the child module object
$childModule = Vtiger_Module::getInstance($childModuleName);
if (!$childModule) {
echo "Child module '$childModuleName' does not exist.";
return;
}
// Retrieve related list information for the given relationship
global $adb;
$query = "SELECT relation_id FROM vtiger_relatedlists WHERE tabid = ? AND related_tabid = ?";
$parentTabId = $parentModule->id;
$childTabId = $childModule->id;
$result = $adb->pquery($query, array($parentTabId, $childTabId));
if ($adb->num_rows($result) > 0) {
$relationId = $adb->query_result($result, 0, 'relation_id');
// Remove the relationship using the relatedlist ID
$adb->pquery("DELETE FROM vtiger_relatedlists WHERE relation_id = ?", array($relationId));
echo "The 1:M relationship between '$parentModuleName' and '$childModuleName' has been removed successfully.";
} else {
echo "No custom 1:M relationship found between '$parentModuleName' and '$childModuleName'.";
}
}
// Usage: Replace 'CustomModule1' and 'Calendar' with your actual module names
removeCustomRelationship('CustomModule1', 'Calendar');
?>
3. Explanation of the Script
- Vtiger_Module::getInstance: Retrieves the module instance for the parent and child modules. It checks whether both modules exist in the CRM.
- Database Query: Queries the
vtiger_relatedlists
table to find the specificrelation_id
that represents the 1:M relationship between the parent and child modules. - Remove the Relationship: Uses a SQL DELETE statement to remove the entry from
vtiger_relatedlists
, thereby unsetting the 1:M relationship.
4. Where to Place and Execute This Script
- Custom PHP Script: Save this script as a PHP file (e.g.,
removeRelationship.php
) in your Vtiger CRM directory, preferably within a custom scripts folder. - Execution: Execute the script from the command line or a web browser if you have access. Make sure you have the necessary permissions to run the script and access the Vtiger CRM database.
5. Important Considerations
- Backup Your Database: Before making any changes to your Vtiger CRM database, make sure to back up your database. This is crucial in case anything goes wrong or you accidentally delete the wrong relationship.
- Test in a Development Environment: Always test your script in a development or staging environment before deploying it in production.
- Check for Dependencies: Ensure that removing the relationship does not break any functionality in the custom modules or the Calendar module.
6. Additional Tips
- Logging and Error Handling: Add proper logging and error handling to your script to capture any issues or errors during execution.
- Vtlib API Functions: For more complex operations, consider using more advanced Vtlib functions that handle relationship management in a more abstracted way.
Conclusion
To simply remove or unset a 1: M (One-to-Many) relationship among all custom modules (or a specific custom module and also a standard module such as Calendar/Activities) in the case of Vtiger 7, then you have to use PHP scripting along with Vtlib functions. Vtlib offers a group of best functions to handle relationships among module dynamically, including removing or adding relationships by having the best Vtiger hosting solutions.