Linking lists in Orchard CMS

Go To StackoverFlow.com

1

What is the best way to link lists together in Orchard?

For example- I have one companies list and one projects list. When I create project, I want to associate it with a company...ideally, a drop down box pre-populated with the companies that I have created.

What is the best way to go about it?

P.S. I am using Orchard 1.3

Many thanks!

2012-04-04 00:50
by NomadTraveler


0

To see an example of pre-populating a checkbox or droplist in an Editor view, look at the Orchard docs 1-N / N-N example.

To see an example of setting up a relationship between two different parts via a droplist, look at the FeaturedItemSlider or the ContentSlider (which was derived from the former) Orchard modules on Codeplex or the Orchard gallery.

  1. Update ProjectPart to have a property of type CompanyPart (assuming these are the namesyou used for your models).
  2. In Migrations.cs, update the ProjectPartRecord table to have a CompanyPartRecord_Id column of type int. Orchard will set up the mapping with NHibernate so the ProjectPartRecord table can be joined to CompanyPartRecord table.
  3. Update the ProjectPartDriver class constructor to have a parameter of type IRepository. Save the instance as a readonly class property of the same type (named something like _companiesRepo).
  4. Update the ProjectPartDriver .Editor() method to pull a list of Companies from the _companiesRepo.
  5. Update the other ProjectPartDriver .Editor() method (the one that handles postback) to set the ProjectPart's CompanyPart property to the Company selected in the dropdown. You use the company Id from the droplist to look up the CompanyPart from the db -- I use viewModels to make things easier, so my version would look like this: projectPart.CompanyPart = _companiesRepo.Get(companyId)
  6. Update the editorTemplate for ProjectPart to render the droplist.

I'm not sure how you plan to use this relation on the front end. There are two ways to go about it. (1) you can create a custom controller with matching Route, and in the controller have code to look up the parts based on the relationship you've set up, or (2) in the ProjectPartDriver.Display() method you can do the lookup using IContentManager.

2012-04-04 02:28
by Giscard Biamby
Ah, I haven't created a custom module. Just creating lists in CMS. What is the best way to query the lists manually from the view? Write own SQL queries? Is there an elegant way? I am new with MVC :| Thanks - NomadTraveler 2012-04-05 00:47
There are many ways. You can use linq, HQL, or pure SQL. If you're doing a hacky solution the best place to put the query might be the Driver class, in the Display() method. There are some modules, I believe by randompete, that might let you link content parts together without writing code. I haven't used them so I can't give any input. I think it's called ScienceProject, or Mechanics, or something like that - Giscard Biamby 2012-04-05 12:46
Ads