gpt4 book ai didi

Umbraco 关系类型

转载 作者:行者123 更新时间:2023-12-02 04:37:01 24 4
gpt4 key购买 nike

在 Umbraco 6.1.6 的开发人员部分中,有一个Relation Types 节点。

任何人都可以解释什么是关系类型以及它们是否有实际应用。我看过一些文档,但仍然不确定为什么我可能需要使用它们。

它们在 v6 和 v7 中是否仍然相关?

最佳答案

我最近开始记录 Relations Service这应该让您深入了解您可以用它做什么。我偶尔会用它来维护内容树中节点之间的关系。

如果您曾经在 Umbraco 中复制一个节点,您可以选择将新节点与原始节点相关联,该原始节点使用一种称为“复制时关联文档”的关系类型。例如,有了适当的关系,您就可以连接到诸如保存事件之类的事件,并且每当更新父节点时,您也可以更新相关的子节点。这种技术有时用于需要跨每种语言同步内容的多语言站点。

以下是我最近从事的一个项目的缩略示例,在该项目中可能会创建一个重复事件。我们需要知道系列中的第一个事件以及该事件的所有后续事件( child )。

  public class Events : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saved += ContentServiceSaved;
}

private void ContentServiceSaved(IContentService sender, SaveEventArgs<IContent> e)
{
var rs = ApplicationContext.Current.Services.RelationService;
var relationType = rs.GetRelationTypeByAlias("repeatedEventOccurence");

foreach (IContent content in e.SavedEntities)
{
var occurences = rs.GetByParentId(content.Id).Where(r => r.RelationType.Alias == "repeatedEventOccurence");
bool exists = false;

foreach (var doc in occurences.Select(o => sender.GetById(o.ChildId)))
{
// Check if there is already an occurence of this event with a matching date
}

if (!exists)
{
var newDoc = sender.Copy(content, eventsDoc.Id, true, User.GetCurrent().Id);

// Set any properties you need to on the new node
...

rs.Relate(content, newDoc, relationType);
}
}
}
}

关于Umbraco 关系类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com