了解 Orchard 连接和数据关系

发布于 2024-12-23 17:10:40 字数 2359 浏览 2 评论 0原文

在 Orchard 中,模块开发人员如何能够了解“连接”的工作原理,特别是在连接到核心部分和记录时?我见过的更好的帮助之一是 Orchard 文档,但这些例子都没有展示如何与现有或核心部分建立关系。作为我正在寻找的示例,这里是取自工作示例的模块服务代码片段:

_contentManager
    .Query<TaxonomyPart>()
    .Join<RoutePartRecord>()
    .Where(r => r.Title == name)
    .List()

在本例中,自定义 TaxonomyPart 正在与核心 RoutePartRecord 连接代码>.我研究了代码,但看不出 TaxononmyPart 如何“连接”到 RoutePartRecord。同样,从工作代码中,这里是另一个片段驱动程序代码,它将自定义 TagsPart 与核心 CommonPartRecord 相关联:

List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
IEnumerable<TagsPart> parts =
    query.Join<CommonPartRecord>()
    .Where(cpr => cpr.Id != currentItemId)
    .OrderByDescending(cpr => cpr.PublishedUtc)
    .Slice(part.MaxItems);

我认为我可以从前面的任何一个示例中学习如何形成我自己的查询。我这样做了:

List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
var stuff =
    query.Join<ContainerPartRecord>()
    .Where(ctrPartRecord => ctrPartRecord.ContentItemRecord.ContentType.Name == "Primary")
    .List();

我的代码的目的是将找到的内容项限制为仅特定容器(或博客)的内容项。当代码运行时,它在我的连接查询上引发异常,提示 {"could not parse property: ContentType of: Orchard.Core.Containers.Models.ContainerPartRecord"}。这会导致各种问题:

  1. 为什么在第二个示例的驱动程序的 Display() 方法中填充的是 CommonPartRecord,而不是 ContainerPartRecord?一般来说,我如何知道填充了哪些部分记录以及何时填充?
  2. 在工作代码片段中,由于没有指定连接键/条件(并且没有明显的隐式连接键),连接到底如何工作?例如,我检查了数据迁移文件和models类,发现TagsPart和CommonPartRecord之间没有内在联系。因此,除了查看示例代码之外,人们如何首先知道这样的连接是合法的或可能的?
  3. 我尝试使用 TagsPartContainerPartRecord 进行的连接在任何情况下都合法吗?哪个?
  4. 这些示例的查询语法主要是 Orchard、NHibernate 或 LINQ to NHibernate 的反映吗?如果它主要是 NHibernate 的反映,那么推荐阅读哪本 NHibernate 书籍或文章,以便我可以更深入地了解 Orchard?

文档中似乎存在关于此类想法和问题的漏洞,这使得编写模块变得困难。无论这个主题能找到什么答案,我都很乐意将其编译成一篇文章或社区 Orchard 文档。

In Orchard, how is a module developer able to learn how "joins" work, particularly when joining to core parts and records? One of the better helps I've seen was in Orchard documentation, but none of those examples show how to form relations with existing or core parts. As an example of something I'm looking for, here is a snippet of module service code taken from a working example:

_contentManager
    .Query<TaxonomyPart>()
    .Join<RoutePartRecord>()
    .Where(r => r.Title == name)
    .List()

In this case, a custom TaxonomyPart is joining with a core RoutePartRecord. I've investigated the code, and I can't see how that a TaxononmyPart is "joinable" to a RoutePartRecord. Likewise, from working code, here is another snippet driver code which relates a custom TagsPart with a core CommonPartRecord:

List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
IEnumerable<TagsPart> parts =
    query.Join<CommonPartRecord>()
    .Where(cpr => cpr.Id != currentItemId)
    .OrderByDescending(cpr => cpr.PublishedUtc)
    .Slice(part.MaxItems);

I thought I could learn from either of the prior examples of how to form my own query. I did this:

List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
var stuff =
    query.Join<ContainerPartRecord>()
    .Where(ctrPartRecord => ctrPartRecord.ContentItemRecord.ContentType.Name == "Primary")
    .List();

The intent of my code is to limit the content items found to only those of a particular container (or blog). When the code ran, it threw an exception on my join query saying {"could not resolve property: ContentType of: Orchard.Core.Containers.Models.ContainerPartRecord"}. This leads to a variety of questions:

  1. Why in the driver's Display() method of the second example is the CommonPartRecord populated, but not the ContainerPartRecord? In general how would I know what part records are populated, and when?
  2. In the working code snippets, how exactly is the join working since no join key/condition is specified (and no implicit join keys are apparent)? For example, I checked the data migration file and models classes, and found no inherent relation between a TagsPart and a CommonPartRecord. Thus, besides looking at that sample code, how would anyone have known in the first place that such a join was legal or possible?
  3. Is the join I tried with TagsPart and ContainerPartRecord legal in any context? Which?
  4. Is the query syntax of these examples primarily a reflection of Orchard, of NHibernate, or LINQ to NHibernate? If it is primarily a reflection of NHibernate, then which NHibernate book or article is recommended reading so that I can dig deeper into Orchard?

It seems there is a hole in the documentation regarding these kinds of thoughts and questions, which makes it hard to write a module. Whatever answers can be found for this topic, I'd be glad to compile into an article or community Orchard documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-12-30 17:10:40
  1. 连接只是为了启用它后面的位置。这并不意味着正在连接的部分实际上将从数据库中删除。无论最新的 1.x 源代码如何,都会发生这种情况,并且在 1.3 版本中会延迟发生。
  2. 您不需要条件,因为您只能通过这种方式连接零件。连接条件是隐式的:部件通过项目 ID 连接。
  3. 是的。不合法的是,where 中的条件使用的数据无法从连接的零件记录中获得。
  4. 这些示例都是 Orchard Content Manager 查询,因此它们受到相当大的限制,但只要您不超出其边界,它们也相当容易构建,因为可以假设很多事情并且会隐式发生。如果您需要更多控制,可以使用最新 1.x 版本中添加的新 HQL 功能。

至于文档中的漏洞,好吧,但是当然。我们今天拥有的文档仅涵盖了该平台的一小部分。今天最好的参考是源代码。我们和社区其他成员都会高度赞赏您为此做出的任何贡献。如果您需要这方面的帮助,请告诉我。

  1. The join is only there to enable the where that follows it. It doesn't mean that the part being joined will be actually brought down from the DB. That will happen no matter what with the latest 1.x source, and will happen lazily with 1.3.
  2. You don't need a condition as you can only join parts this way. The join condition is implicit: parts are joined by the item id.
  3. Yes. What is not legal is that the condition in the where is using data that is not available from the joined part records.
  4. Those examples are all Orchard Content Manager queries, so they are fairly constrained, but also fairly easy to build as long as you don't step outside of their boundaries because so much can be assumed and will happen implicitly. If you need more control, you could use the new HQL capabilities that were added in the latest 1.x drops.

As for holes in the documentation, well, but of course. The documentation that we have today is only covering a very small part of the platform. Your best reference today is the source code. Any contribution you could make to this is highly appreciated by us and by the rest of the community. Let me know if you need help with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文