Jump to content

How to create many-to-many relationship


binitprads
 Share

Recommended Posts

I am starting to use processwire and really liking it but having one issue which i wanted to ask help for. i am working on a directory type of site that consists of two types of information groups for now:

 
  • Organizations (different types of organizations in different industries)
  • People (different types of people who are in most cases affiliated with the organizations above)
 
I want to create a relationship between the two types so i can list different people that are (or were) part of the organization and was initially able to do this simply by doing a page (lookup) field which i included in one of the above templates/pages to look up the other group. E.g. lookup people in the organization page. 
 
However this was too simple for my needs and i also wanted to be able to put related information (e.g. the person's title at the organization, when they started, when they left etc). So an intermediate type of place would be needed to store this which i was not sure how to do. So it would have to be a many-to-many relationship where more attributes about the relationship can be added. I also wanted an easy way to add more relationships (e.g. from the organization page, i want to be able to add more people that work or worked for the organization and also add more details like the title they have in the organization etc). 
 
Similarly also under a person's profile, i want to list the organizations he/she is (or was affiliated with). 
 
I know this is possible in PW, if someone could please point a novoice like me in the right direction on how to do this or please let me know if more information is needed. 
Thanks,  Binit
 
  • Like 1
Link to comment
Share on other sites

you can use the pagetype field for all these relations. It's just a mater of organizing your pages accordingly. So, taking your example, you could organize your PW tree like this:

-people

--john

--doe

--xico

--...

-organizations

--acme

--corporation

--lda

--...

-industries

--candies

--guns

--moon trips

--...

-types of people

--nice

--bad

--so so

--...

Then, on the template for organizations you put a page field with "industries" as parent of the selectable pages (PSP), and on the template for people you put two page fields, one with "types of people" as the PSP, and the other with organization as the PSP. You can make this system grow indefinitely  Just think that any time you want a dropdown, or a selector for multiple choices in your templates, this is the way to do it in PW, even if those pages only exist to be a selectable value on other pages.

Edit: And welcome to the forum :)

  • Like 4
Link to comment
Share on other sites

Thanks diogo very much for the info I am going to try it out. 

I just had one additional question, in the example you gave it would be able to do something like this right?

- Organization 1

 -- Mr Jon, Title=CEO, Start year=2010, End Year=2012, Note=<some free text about his time here>

 -- Mr Xico Title=MD, Start year=2011, Note=<some free text> 

- Organization 2

  -- Mr Jon, Title=Board Member, Start year=2009

With the above setup i wanted to have people profile pages that could show the relation in the following way: 

- Person: Mr Jon

   -- Organization 1, CEO, 2010-2012

   -- Organization 2, Board Member, 2009-current

In the above example including the example layout you had given, where would be best to store the additional things related to the relationship between person and organization (i..e. title, start year, end year etc) some of which might be free text fields? and also have an easy way to input them when adding the organization or person.

Link to comment
Share on other sites

You could accomplish the above in various ways. I'll cover two:

Option 1: Structure

-people

--john

---organization 1, ceo, 2010-2012

---organization 2, board member, 2009-current

--mike

--etc.

The above would have child pages of each person that represents their organization connections. The fields on that template would be:

  • organization (single page select to /organizations/)
  • position or title (text, their position at the organization)
  • start_year (integer)
  • end_year (integer)

Option 2: Repeater

This is the option I would use, and it's perfect for this sort of thing. It's basically doing the same thing as option 1, but making it simpler and disconnecting it from the structure. You'd create a new field using ProcessWire's repeater fieldtype, perhaps naming it 'organizations' or 'person_organizations'. Then add the same fields mentioned in the bulleted list above. Add this new 'person_organizations' field to your 'person' template. Now you can define as much meta information for that person<->organization relationship as you want to. 

More on how to use ProcessWire's Repeater fieldtype

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...