Relationship between project and companies

Hello Everyone,

I have an issue on assigning a project to a specific company.

In the project module I’ve added a relate field linked to the companies modules.
When creating or editing the project, we can select a company from our list within that field.

That project however is then not shown under the company in the company project subpanel. To be shown there, I either have to create the project starting from the company module project subpanel, or add the company in the company subpanel of the project detail.

Is there a way to use my custom field to link the project to the specific company?

Thanks!

Hi, welcome to the Community! :tada:

I see from Studio that there is already a projects_accounts many-to-many relationship in place. Unfortunately, for some reason this is not directly editable from the UI, so you don’t get much control over it.

With some under the hood tweaking it should be possible to make that visible from the edit views. I wish I could give you a more user-friendly answer on this…

Maybe some of the many talented developers that visit these forums can help you out with specific advice on what to change…

1 Like

Hi pgr,

Thanks for your answer.

Not being editable from studio is not an issue, my install is a self hosted one that i’m setting up for my company, so i have full access to both the db and the suitecrm files.
I’m not a developer but i have a basic IT knowledge and in general if i look at some code i can get a gross understanding of what it does.
I’ve tried to look at both files and db and so far I’ve understood that the relationship between the two should be built around the account_id and project_id fields, and my custom account field inside the project is linked to the account module but to the account name and not account_id.

If i’m right, is there a way to create an hidden link between my account field and the account_id field needed to create the connection?

Many thanks.

I can’t give you the details because it would take a deeper study to get them, and I don’t have the time. But my idea is this: if you look at the way you can select an Account in the contact’s edit view, for example, you see that it’s a relate field, and you select by account name, but underneath it’s storing the account id.

You should be able to get a similar Relate field behavior with the existing relationship. Normally that all gets created automatically by Studio when you create Relate field.

What I don’t know exactly is how to get that working after the relationship already exists.

It might be easier to just a new Relate field (thus creating a new relationship)

You might be able to create the relationship by using a workflow or a after save logic hook…

@Salonick
If I understand correctly you want to make relationship project one-to-many accounts.
Without Studio you should make a few files. There is working example here.
After files maked do ‘Quick Repair and Rebuild’ and when it will finishe to scroll window down and push button “Execute”

  1. custom/Extension/application/Ext/TableDictionary/project_accounts.php
<?php 
include('custom/metadata/project_accountsMetaData.php');
?>
  1. custom/metadata/project_accountsMetaData.php
<?php
$dictionary["project_accountsMetaData"] = array (
  'true_relationship_type' => 'one-to-many',
  'relationships' => 
  array (
    'project_accounts' => 
    array (
      'lhs_module' => 'Project',
      'lhs_table' => 'project',
      'lhs_key' => 'project_accounts_id',
      'rhs_module' => 'Accounts',
      'rhs_table' => 'accounts',
      'rhs_key' => 'id',
      'relationship_type' => 'one-to-many',
    ),
  ),
);
  1. custom/Extension/modules/Accounts/Ext/Layoutdefs/project_accounts.php
<?php
$layout_defs["Accounts"]["subpanel_setup"]['project_accounts'] = array (
  'order' => 110,
  'module' => 'Project',
  'subpanel_name' => 'default',
  'sort_order' => 'asc',
  'sort_by' => 'id',
  'title_key' => 'LBL_ACCOUNT_PROJECTS_TITLE',
  'get_subpanel_data' => 'project_accounts',
  'top_buttons' => 
  array (
    0 => 
    array (
      'widget_class' => 'SubPanelTopButtonQuickCreate',
    ),
    1 => 
    array (
      'widget_class' => 'SubPanelTopSelectButton',
      'mode' => 'MultiSelect',
    ),
  ),
);
  1. custom/Extension/modules/Accounts/Ext/Language/en_us.accounts_project.php
<?php
$mod_strings['LBL_ACCOUNT_PROJECTS_TITLE'] = 'Projects (one --- many)';
  1. custom/Extension/modules/Accounts/Ext/Vardefs/accounts_project.php
<?php
$dictionary["Account"]["fields"]["project_accounts"] = array (
  'name' => 'project_accounts_s',
  'type' => 'link',
  'relationship' => 'project_accounts',
  'source' => 'non-db',
  'module' => 'Project',
  'bean_name' => 'Project',
  'side' => 'right',
  'vname' => 'LBL_ACCOUNTS_PROJECT_TITLE',
);
  1. custom/Extension/modules/Project/Ext/Language/en_us.project_accounts.php
<?php
$mod_strings['LBL_PROJECT_ACCOUNTS_TITLE'] = 'Account';
  1. custom/Extension/modules/Project/Ext/Vardefs/project_accounts.php
<?php
$dictionary["Project"]["fields"]["project_accounts"] = array (
  'name' => 'project_accounts',
  'type' => 'link',
  'relationship' => 'project_accounts',
  'source' => 'non-db',
  'module' => 'Accounts',
  'bean_name' => 'Account',
  'vname' => 'LBL_PROJECT_ACCOUNTS_TITLE',
  'id_name' => 'project_accounts_id',
);
$dictionary["Project"]["fields"]["project_accounts_name"] = array (
  'name' => 'project_accounts_name',
  'type' => 'relate',
  'source' => 'non-db',
  'vname' => 'LBL_PROJECT_ACCOUNTS_TITLE',
  'save' => true,
  'id_name' => 'project_accounts_id',
  'link' => 'project_accounts',
  'table' => 'accounts',
  'module' => 'Accounts',
  'rname' => 'name',
  'dbType' => 'varchar',
);
$dictionary["Project"]["fields"]["project_accounts_id"] = array (
  'name' => 'project_accounts_id',
  'type' => 'id',
  'rname' => 'id',
  'dbType' => 'id',
  'relationship' => 'project_accounts',
  'reportable' => false,
  'side' => 'right',
  'module' => 'Accounts',
  'vname' => 'LBL_PROJECT_ACCOUNTS_TITLE',
);

@Salonick
I forgot about:

  • custom/modules/Project/metadata/editviewdefs.php
    and
  • custom/modules/Project/metadata/detailviewdefs.php
    Add to any panel:
...
        4 =>
          array(
              0=>array(
                  'name'=>'project_accounts_name'
                  ),
        ),
...

Thanks everyone for the answers, I’ve been able to link the project to the company with a one to many relationship added in the account module and add few more links through Studio.

I still have doubts on the many to many relationship though:

My understanding is it’s like this:

  • one to one is a single relate field in each module
  • one to many is a single relate field on the one side and a subpanel on the many
  • many to many is two subpanel only.

I’m asking again confirmation because I’ve created a custom module to track sample shipment, and each shipment may contain more than one product.

I’ve tried with a one (product) to many (sampling) and it was working as a concept, product field within the sample details and sampling list subpanel under the product code.
The issue come from needing N of those product field within a single sample shipment, but replicating the relationship added the fields but also other subpanels.

To clarify more, the N product field within the samples are “needed” because for each product i have other data to specify that may vary each time despite product being the same, not something i can do in the subpanel because it’s just a collection of links.

Right now i added the many to many to create the link for sample search by product purpose, and use related + text field for the data in the sample details.

If i understood correctly, it’s not possible to link a relate field to a many to many because the many to many is an ID stored in an external table, neither is possibile to link multiple relate field of the one side to a single many subpanel.

Have i got it?

Thanks again,

@Salonick
Do you want to use Studio only?
If you can make custom php code look at the screenshot and the post:


@p.konetskiy
it’s not me wanting to use only studio, it’s more studio being the only tool that I can use reliably.

My php knowledge is close to 0 and if I find detailed instruction like you posted before I can edit the code.
I was able to add print to pdf function to the project module by following a post from here on the forum, and it works.
But going totally custom from 0 it’s more a random shot, may work, may not.

I’ve read your post, if I understood it, it creates a collection field that can contain other kind of fields, correct? But the field contained in the collection, can be relationship fields?

Thanks.

@Salonick
Yes. The type collection can use another standard types of fields include relationship.