Custom relationship between Employees and Documents

Hi,

i’m trying to create a (m2m) relationship between the Employees module and the Document modules,

I know that the Employees module doesn’t have a database table but shares data with the Users module.

How can i create such relationship? Should i create the relationship on the Users module an then create a custom subpanel on Employees?

Thanks

This sounds sensible, try it and tell us how it went :slight_smile:

I created the relationship between Users and Documents and the custom subpanel in Documents:

custom/Extension/modules/Documents/Ext/LayoutDefs/documents_employees_1_Documents



<?php

$layout_defs["Documents"]["subpanel_setup"]['document_employees'] = [
  'order' => 100,
  'module' => 'Employees',
  'subpanel_name' => 'employees',
  'get_subpanel_data' => 'function:getEmployeesForDocument',
  'generate_select' => true,
  'title_key' => 'LBL_DOCUMENT_EMPLOYEES',
  'top_buttons' => [],
  'function_parameters' => [
	  'import_function_file' => 'custom/modules/Documents/getEmployeesForDocument.php',
	  'document_id' => $this->_focus->id,
	  'return_as_array' => 'true'
  ]
];

custom/modules/Documents/getEmployeesForDocument.php


<?php

function getEmployeesForDocument($params) {

	$args = func_get_args();

	$documentId = $args[0]['document_id'];

	return [
		'select' => " SELECT users.*",
		'from' => " FROM users",
		'where' => " WHERE users.deleted = '0'",
		'join' => " INNER JOIN users_documents_1_c ON users_documents_1_c.users_documents_1users_ida = users.id AND users_documents_1_c.deleted = '0'"
			. " INNER JOIN documents ON documents.id = users_documents_1_c.users_documents_1documents_idb AND documents.deleted = '0' AND documents.id = '$documentId'",
		'join_tables' => []
	];

}

but i am getting the following exception when trying to open the subpanel:


PHP Fatal error:  Uncaught Error: Call to a member function isCollection() on boolean in /home/www/data/SugarBean.php:639\nStack trace:\n#0 /home/www/include/ListView/ListView.php(1102): SugarBean::get_union_related_list(Object(Document), 'id', 'asc', '', '', -1, 10, '', false)\n

#1 /home/www/include/ListView/ListViewSubPanel.php(142): ListView->processUnionBeans(Object(Document), false, '_CELL')\n
#2 /home/www/include/SubPanel/SubPanel.php(220): ListViewSubPanel->process_dynamic_listview('Documents', Object(Document), false)\n
#3 /home/www/include/SubPanel/SubPanel.php(231): SubPanel->ProcessSubPanelListView('include/SubPane...', Array)\n
#4 /home/www/include/SubPanel/SubPanelViewer.php(95): SubPanel->display()\n
#5 /home/www/include/MVC/Controller/SugarController.php(936): require_once('/home/www/inclu...')\n
#6 /home/www/include/MVC/Controller/SugarController.php(402): SugarController->handleActionMaps()\n
#7 /home/www/include/MVC/Controller/SugarController.php(315): SugarController->process()\n
#8

Before we start dealing with the custom subpanel attempt, did it work well without any custom subpanel? Did a default subpanel appear?

Yes, the Users subpanel appeared in the Document detail view and vice versa

Sorry, it’s hard to help without trying to build your full solution on my test system, and I don’t have much time for that now…

I would try starting from the working situation, with the code done by Studio, and then changing small things one by one, and testing each time to see when it breaks…