Content Parts in Content Types

Topics: Writing modules
Jun 1, 2012 at 7:01 PM

I'm finally updating my module to use Content Parts and Content Types instead of being completely stand-alone modules and have a quick question about what Parts I need to include in a Type.

Say I have the following setup:

Show Type: Show Part, Common Part
Season Type: Season Part, Show Part, Common Part
Episode Type: Episode Part, Season Part, Show Part, Common Part

My question is on the Episode Type...do I need to include the Show Part or am I able to get to the Show Part simply by including the Season Part?

It works like this with the way I have it setup now (not using Content Types and Parts), but I'm not sure if it also works this way when using them. Basically, I'm able to climb the hierarchy right now by going:  Episode.Season.Show.ShowName

Thanks for the assistance.

Coordinator
Jun 1, 2012 at 8:50 PM

Seems like you are confusing type and part. The season part does not have the show part, because parts don't have parts. Types have parts. Or you are not giving enough information. Are there relationships between those parts? If so, why would the season type need a show part for example?

Jun 1, 2012 at 9:53 PM

You're right...as soon as you said that, it clicked that it's not right. There aren't relationships between those parts.

The way it works, when creating a Season, I need to select which Show it belongs to...and when modifying the Season, I'd like to see the Show information.

Similarly, when creating a Episode, I need to select which Show/Season it belongs to

So...it sounds like I'm still not understanding how Types and Parts work together.

Based on what I described above, can you point me in the right direction on how to organize those models?

I swear I'm actually going to grasp this concept eventually...probably the hardest thing to get my mind around with Orchard since it's such a departure from the typical structure I'm used to working in. Thanks =)

Coordinator
Jun 1, 2012 at 9:55 PM

 

http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations

Jun 1, 2012 at 10:06 PM

So even though I created a ShowPart and ShowPartRecord, I can include the ShowPartRecord in the SeasonPartRecord? As such?

using Orchard.ContentManagement.Records;

namespace Raptor.MyRealityPicks.Models {
    public class SeasonPartRecord : ContentPartRecord {
        public virtual int Number { get; set; }
        public virtual string Name { get; set; }
        public virtual ShowPartRecord ShowPartRecord { get; set; }
        public virtual bool Active { get; set; }
    }
}


using Orchard.ContentManagement;

namespace Raptor.MyRealityPicks.Models {
    public class SeasonPart : ContentPart {
        public int Number{
            get { return Record.Number; }
            set { Record.Number = value; }
        }
        public string Name {
            get { return Record.Name; }
            set { Record.Name = value; }
        }
        public ShowPartRecord Show {
            get { return Record.ShowPartRecord; }
            set { Record.ShowPartRecord = value; }
        }
        public bool Active {
            get { return Record.Active; }
            set { Record.Active = value; }
        }
    }
}

Coordinator
Jun 1, 2012 at 11:40 PM

Pretty much, but an important part of the puzzle is the migration code.

Jun 29, 2012 at 9:51 PM

I'm working through this using the documentation and am doing good so far, but have a question regarding multiple relationships to the same Part

Let's say in my model, I want to include 4 UserPart relationships...

        public virtual UserPartRecord RequestUser { get; set; }
        public virtual UserPartRecord AssignedUser { get; set; }
        public virtual UserPartRecord CreateUser { get; set; }
        public virtual UserPartRecord LastUpdateUser { get; set; }

In my migrations, is it correct to define the columns as:

                    .Column("RequestUser_Id")
                    .Column("AssignedUser_Id")
                    .Column("CreateUser_Id")
                    .Column("LastUpdateUser_Id")

Is that going to properly map those back to the UserPartRecord table?

Jun 29, 2012 at 11:13 PM

I should have scrolled to the next example for relating Content Parts to Content Parts first...let me go through that since that's what I'm doing and see if I can figure it out first. Thanks.

Jun 29, 2012 at 11:46 PM
Edited Jun 29, 2012 at 11:47 PM

I can't seem to get the migration code figured out...

Can someone help me with figuring out how to set that up?

If my model setup looks like this

 

using Orchard.ContentManagement.Records;

namespace Raptor.MyRealityPicks.Models {
    public class SeasonPartRecord : ContentPartRecord {
        public virtual int Number { get; set; }
        public virtual string Name { get; set; }
        public virtual ShowPartRecord ShowPartRecord { get; set; }
        public virtual bool Active { get; set; }
    }
}


using Orchard.ContentManagement;

namespace Raptor.MyRealityPicks.Models {
    public class SeasonPart : ContentPart {
        public int Number{
            get { return Record.Number; }
            set { Record.Number = value; }
        }
        public string Name {
            get { return Record.Name; }
            set { Record.Name = value; }
        }
        public ShowPartRecord Show {
            get { return Record.ShowPartRecord; }
            set { Record.ShowPartRecord = value; }
        }
        public bool Active {
            get { return Record.Active; }
            set { Record.Active = value; }
        }
    }
}

 

Does my migration look like this?

 

using System;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Raptor.MyRealityPicks.Models;

namespace Raptor.MyRealityPicks {
    public class Migrations : DataMigrationImpl {
        public int Create() {
            SchemaBuilder.CreateTable("ShowPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("Name", column => column.WithLength(50))
            );

            SchemaBuilder.CreateTable("SeasonPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("Number")
                    .Column<string>("Name", column => column.WithLength(50))
.Column<bool>("Active")
.Column<int>("ShowPartRecord_Id")
            );

            return 1;
        }
    }
}
Developer
Jun 29, 2012 at 11:58 PM

Looks right to me. Why, isn't it working?

Jun 30, 2012 at 12:25 AM

Whenever I try to display the records in Season, nHibernate throws a big ole error.

If I remove the references to ShowPartRecord in the model, it displays fine...so I'm assuming it's somewhere in the join. That's why I thought the migration was wrong.

Jun 30, 2012 at 12:30 AM

GRRR...nevermind. I had a column name misspelled in the migration. The stack trace from debugging was not even close in it's error description. I looked at the log file instead and the error is spelled out right there.

Sometimes debugging is more of a pain than it's worth =) LOL

Developer
Jun 30, 2012 at 12:31 AM

What's the exact error? Maybe try deleting all of the Season and ShowPart records?

Developer
Jun 30, 2012 at 12:35 AM

Ah, yeah I've been bitten by that before :)