{"id":9139,"date":"2024-09-16T08:23:04","date_gmt":"2024-09-16T08:23:04","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9139"},"modified":"2024-09-16T08:36:14","modified_gmt":"2024-09-16T08:36:14","slug":"remove-1-custom-module-relationship-in-vtiger-with-php","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/remove-1-custom-module-relationship-in-vtiger-with-php\/","title":{"rendered":"Remove 1 Custom Module Relationship in Vtiger with PHP"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,856<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p class=\"wp-block-paragraph\">To unset a <strong>One-to-Many (1:M)<\/strong> relationship between custom modules and the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">ModComments<\/mark><\/code> module in Vtiger CRM 7 using VTlib functions in a PHP script, you can use the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Vtiger_Relation::delete()<\/mark><\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s how you can remove (unset) a custom 1:M relationship between your custom modules and the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">ModComments<\/mark><\/code> module:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Script to Unset 1:M Relationship:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">&lt;?php\n\/\/ Include the vtlib library (Make sure to adjust the path according to your vtiger CRM installation)\ninclude_once 'vtlib\/Vtiger\/Module.php';\n\n\/\/ Define your custom module name and related module name\n$moduleName = 'CustomModule'; \/\/ Replace 'CustomModule' with your actual custom module name\n$relatedModuleName = 'ModComments'; \/\/ The related module name (in this case, ModComments)\n\n\/\/ Get instances of both modules\n$moduleInstance = Vtiger_Module::getInstance($moduleName);\n$relatedModuleInstance = Vtiger_Module::getInstance($relatedModuleName);\n\nif ($moduleInstance &amp;&amp; $relatedModuleInstance) {\n    \/\/ Find the relationship between the modules\n    $relation = Vtiger_Relation::getInstance($moduleInstance, $relatedModuleInstance);\n\n    if ($relation) {\n        \/\/ Delete (unset) the 1:M relationship\n        $relation-&gt;delete();\n        echo \"1:M relationship between $moduleName and $relatedModuleName has been unset successfully.\";\n    } else {\n        echo \"No existing relationship found between $moduleName and $relatedModuleName.\";\n    }\n} else {\n    echo \"Error: Module instances could not be retrieved.\";\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Module Instances<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Vtiger_Module::getInstance($moduleName)<\/mark><\/code>: Retrieves the instance of your custom module (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">CustomModule<\/mark><\/code>).<\/li>\n\n\n\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Vtiger_Module::getInstance($relatedModuleName)<\/mark><\/code>: Retrieves the instance of the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">ModComments<\/mark><\/code> module.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     2. <strong>Relationship Instance<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Vtiger_Relation::getInstance($moduleInstance, $relatedModuleInstance)<\/mark><\/code>: This fetches the relationship instance between the two modules.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    3. <strong>Delete the Relationship<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Vtiger_Relation::delete()<\/mark><\/code>: This method removes (unsets) the 1:M relationship between the two modules.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important Notes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CustomModule<\/strong>: Replace <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">'CustomModule'<\/mark><\/code> with the name of your actual custom module.<\/li>\n\n\n\n<li><strong>Backup<\/strong>: Before running any script, ensure that you back up your CRM database, as changes to relationships might have cascading effects.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To unset a\u00a0One-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 <a href=\"https:\/\/www.infinitivehost.com\/managed-vtiger-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>best Vtiger hosting solutions<\/strong><\/mark><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,856 Views 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\u2019s how you can remove (unset) a custom 1:M relationship between your custom modules and the ModComments module: PHP Script to Unset 1:M [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[204],"tags":[],"class_list":["post-9139","post","type-post","status-publish","format-standard","hentry","category-vtiger-solutions"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9139"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9139\/revisions"}],"predecessor-version":[{"id":9144,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9139\/revisions\/9144"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}