動的に作成して移行のコンテンツ タイプにアタッチしたアタッチ パーツの DateTimeField から DateTime を取得できません。
構造は次のようになります。
CourseDate (ContentType)
ProductPart (attached part from other project)
CourseDatePart (attached part)
TimeSpanPart (attached part from "dynamically" created part)
DateTimeField である StartDateTime と EndDateTime を TimeSpanPart から取り出そうとしています。ただし、DateTime プロパティは常に 1/1/0001 です
値を取得するために使用しているコードは次のとおりです。TimeSpanPart 以外はすべて機能します。
private EditCourseViewModel BuildEditorViewModel(CoursePart part)
{
var courseDates = _contentManager.Query<CourseDatePart, CourseDatePartRecord>().Where(x => x.CourseId == part.Id)
.List<CourseDatePart>()
.Select(x =>
new CourseDateViewModel
{
Id = x.Id,
StartDate = ((dynamic) x.ContentItem).TimeSpanPart.StartDateTime.DateTime,
EndDate = ((dynamic) x.ContentItem).TimeSpanPart.EndDateTime.DateTime,
Sku = ((dynamic) x.ContentItem).ProductPart.Sku,
IsDigital = ((dynamic) x.ContentItem).ProductPart.IsDigital,
Inventory = x.Inventory
}
);
x.Record.ContentItemRecord.Data の値を調べると、必要なデータがあることがわかります。
<Data><TimeSpanPart><StartDateTime>2013-09-02T06:20:00.0000000</StartDateTime><EndDateTime>2013-09-21T00:05:00.0000000</EndDateTime></TimeSpanPart></Data>
移行の関連部分は次のとおりです。
ContentDefinitionManager.AlterPartDefinition("TimeSpanPart", part=>part
.Attachable()
.WithField("StartDateTime", f=>f.OfType("DateTimeField").WithDisplayName("Start Date Time"))
.WithField("EndDateTime", f=>f.OfType("DateTimeField").WithDisplayName("End Date Time"))
);
ContentDefinitionManager.AlterTypeDefinition("CourseDate", type=>type
.WithPart(typeof(CourseDatePart).Name)
.WithPart("TimeSpanPart")
.WithPart(typeof(ProductPart).Name)
);
編集:
フィールドのストレージを管理する Orchard InfosetStorageProvider クラスをもう少し掘り下げました。BindStorage メソッドでは、Data XML 要素を Get メソッドに渡しています。しかし、infosetPart.Infoset.Element (上に示したデータが必要です) を渡す代わりに、null ではないため、infosetPart.ContentItem.VersionRecord を渡しています。代わりに空の XML 要素です: <Data />
return new SimpleFieldStorage(
(name, valueType) => Get(infosetPart.ContentItem.VersionRecord == null ? infosetPart.Infoset.Element : infosetPart.VersionInfoset.Element, partName, fieldName, name),
(name, valueType, value) => Set(infosetPart.ContentItem.VersionRecord == null ? infosetPart.Infoset.Element : infosetPart.VersionInfoset.Element, partName, fieldName, name, value));
}
データを infosetPart.ContentItem.VersionRecord に正しく入力するにはどうすればよいですか? これは、null の代わりに空の xml <Data /> 要素を渡して、代わりに infosetPart.Infoset.Element を Get メソッドに渡すという Orchard のバグですか、それとも何か間違っていますか? infosetPart.ContentItem.VersionRecord と infosetPart.Infoset.Element の違いがわかりません。