relation possibility from contentrecord to Content Item

Topics: General, Writing modules
Nov 6, 2011 at 9:05 AM

I've have read http://orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx#Building_a_Relation_Between_Content_Items_35

but i wonder if it is also possible to have a contentrecord without a part and having a relation from this record to a contentitem directly?

or does that contentrecord really must have a part and belonging contenttype?

Coordinator
Nov 7, 2011 at 7:17 PM

No, it doesn't have to be a content part record.

Nov 7, 2011 at 7:29 PM
Edited Nov 7, 2011 at 7:32 PM

ok cool but is it also possible to automatically let orchard fill the related item without a manual get (by id) in the handler?

so it is possible to have a record without a part and contains 2 relational contentItem properties. so when i query this record with a repository<recorname>() i directly get the related contentitems?

Coordinator
Nov 7, 2011 at 8:27 PM

Yes, that should work, I think.

Nov 8, 2011 at 12:30 AM

AFAIK, Orchard won't load the whole content item in this case, you'll still have to go thru Content Manager to get the item and all its parts (unless I missed something).

Nov 8, 2011 at 7:36 AM

@randompete i also thought that.

So the best thing for me to do is create just a contentType for the n-n relationsship. couple table.

This way i managed to get it working but because i don't every add parts to this contentType i prefer just a record instead of whole contentitem.

Nov 8, 2011 at 8:19 PM

Update:

Ok i tested some things and i came to the following conclusion:

When i want to directly retrieve full contentItems from a relation without a manual contenItem query, i have to use a contentItem and part which contains the relations. This way i can retrieve the full ContentItems with the handler for example by lazyloading.

However when you directly reference a contentItem in a record and query this record, i only retrieve the contentItemRecord and not the full contentItem. So yes it is possible but you only retrieve the contentItemRecord en not the ContentItem.

 

 

Coordinator
Nov 8, 2011 at 8:26 PM

But of course, if you want content items you need to go through content manager. Now I'm completely confused about what you're trying to do. You said earlier that you *didn't* want a part, just records.

Nov 8, 2011 at 8:53 PM

sorry i'm not clear enough. my bad!

What i'm trying to say:

  1. I want to have a couple table for a n-n relationship.
  2. The related items in this table must be contentItems which must be automatically converted to real contentItem when i query this table/data.
  3. I prefer just a record and not a contentitem because i don't want to add additional parts to this contenItem because it's just a couple table between other contentItems.

about 3: Because i found out it is not possible to automatically retrieve the full contenItems by just using a record i have to use a contentItem as couple table.

Conclusion:

I created my couple table as a contentitem and used the lazy loading technique described in the tutorial http://orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx#Building_a_Relation_Between_Content_Items_35

Thanks for your support! I learned so much from it and love Orchard each day more and more!

Coordinator
Nov 8, 2011 at 8:58 PM

Oh, ok, so you have records pointing to content items... Yeah, that's a little atypical. Never tried it. Might be ways to optimize this with the new multi-id methods on content manager, albeit in a more manual way than letting nHib just follow the relationships.

Nov 8, 2011 at 9:27 PM

Yes indeed. It's maybe atypical but i find it very handy. I'll give a basic example for others:

Let say we have a Team ContentType and the default User ContentType. The user-team subscribtions are registered in the earlier called couple table which is a content item of type UserTeamConnections.

I've added a custom part named UserConnectedTeams to the User ContentType. this part contains a List <UserTeamConnections> whichs is related to contenttype UserTeamConnections which is lazy loaded.

private readonly LazyField<IList<UserTeamConnections>> _connectedTeams = new LazyField<IList<UserTeamConnections>>();
        public LazyField<IList<UserTeamConnections>> ConnectedTeamsField { get { return _connectedTeams; } }
        public IList<UserTeamConnections> ConnectedTeams
        {
            get { return _connectedTeams.Value; }
            set { _connectedTeams.Value = value; }
        }

In the handler of this part (UserConnectedTeams) i manually fill up the list of UserTeamConnections by a where statement like:

 

.Where(x => x.User.Id == part.Id).List().Select(s => s).ToList());

 

The nice thing about this setup is that i now can do the following:

userPart.As<SportsTeamItemConnectedTeamsPart>().ConnectedTeams.Select(s=> s.As<RoutePart>().Title);

this can be done without doing a manual content query (based on retrieved ids) with _contentManager.GetMany...