To unset a One-to-Many (1:M) relationship between custom modules and the ModComments
module in Vtiger CRM 7 using VTlib functions in a PHP script, you can use the Vtiger_Relation::delete()
function.
Here’s how you can remove (unset) a custom 1:M relationship between your custom modules and the ModComments
module:
PHP Script to Unset 1:M Relationship:
<?php
// Include the vtlib library (Make sure to adjust the path according to your vtiger CRM installation)
include_once 'vtlib/Vtiger/Module.php';
// Define your custom module name and related module name
$moduleName = 'CustomModule'; // Replace 'CustomModule' with your actual custom module name
$relatedModuleName = 'ModComments'; // The related module name (in this case, ModComments)
// Get instances of both modules
$moduleInstance = Vtiger_Module::getInstance($moduleName);
$relatedModuleInstance = Vtiger_Module::getInstance($relatedModuleName);
if ($moduleInstance && $relatedModuleInstance) {
// Find the relationship between the modules
$relation = Vtiger_Relation::getInstance($moduleInstance, $relatedModuleInstance);
if ($relation) {
// Delete (unset) the 1:M relationship
$relation->delete();
echo "1:M relationship between $moduleName and $relatedModuleName has been unset successfully.";
} else {
echo "No existing relationship found between $moduleName and $relatedModuleName.";
}
} else {
echo "Error: Module instances could not be retrieved.";
}
Explanation:
- Module Instances:
Vtiger_Module::getInstance($moduleName)
: Retrieves the instance of your custom module (CustomModule
).Vtiger_Module::getInstance($relatedModuleName)
: Retrieves the instance of theModComments
module.
2. Relationship Instance:
Vtiger_Relation::getInstance($moduleInstance, $relatedModuleInstance)
: This fetches the relationship instance between the two modules.
3. Delete the Relationship:
Vtiger_Relation::delete()
: This method removes (unsets) the 1:M relationship between the two modules.
Important Notes:
- CustomModule: Replace
'CustomModule'
with the name of your actual custom module. - Backup: Before running any script, ensure that you back up your CRM database, as changes to relationships might have cascading effects.
- This method works for relationships that were created using VTlib. If the relationship was custom-coded or manually created via database modifications, additional adjustments may be needed.
Conclusion
To unset a One-to-Many (1:M) relationship among several the ModComments module and custom modules specifically in the case of Vtiger CRM 7 with the help of VTlib functions in a PHP script, you can simply utilize the Vtiger_Relation::delete() function. Here are some steps mentioned so you can remove (unset) a custom 1:M relationship among your ModComments module and custom modules by having the best Vtiger hosting solutions.