问题
I have a complex scenario that I'm trying to model using Orchard CMS parts.
Now here it is the simplified version to make clear my question.
I have a part name, as example, PersonPart that has just one property: Name. I have another part that contains the person role, name it PersonRolePart and has just one property, Role
In Orchard I have created all the appropriate plumbing (Handlers, Drivers, Views...)
In migrations I created a new content type named Person that contains the two parts.
ContentDefinitionManager.AlterTypeDefinition("Person", cfg => cfg
.WithPart("PersonPart")
.WithPart("PersonRolePart")
.WithPart("CommonPart")
.Creatable(false) );
So far so good, I can create a new person and edit both parts.
Another part that I have is a ServicePart that is bound to one of the PersonRoleParts defined above.
Now the question:
For reporting purpose I need to get all services by PersonRole and get the person details that belong to that role or, in other words, get all the (only one indeed) PersonPart that is used in the Person compound type defined above.
How to do that?
Now in a non-orchard world I would create a simple 1:1 relationship between the 2.
My (failed) attempt so far was to add a PersonRoleRecord_Id field to PersonPartRecord and a PersonRecord_Id to Person role... but I have no idea how to set to correct id on driver or handler since both see just the own part.
Is it possible from driver get an instance of the other fellows parts in content type?
Merge Person and Role is not possible. The scenario is more complex than that and I need same Person Part an 3 different Role-like part for different purposes and I want to avoid duplicate common person data 3 times.
Another idea was to create an appropriate handler but I do not know how to create an Handler for a virtual content type like the one I did.
回答1:
I have managed to solve using the advice on another question that addressed a different problem (validation). Orchard CMS - determining if Model is valid in Content Item Driver
So my solution was to add an handler to both and cast the part and set the appropriate reference to the other part.
OnPublishing<PersonPart>((context, part) =>{
var person = part.As<PersonPart>();
var role= part.As<PersonRolePart>();
if (person != null && role != null) {
if (role.Person == null) {
role.Person = person.Record;
}
}
});
In this specific case since it is a 1:1 relation so i I use just that combined part, both id are the same, at least if the role is just of one type. I will see when I will create more role-like parts.
来源:https://stackoverflow.com/questions/19242539/orchard-connected-parts