了解果园连接和数据关系 [英] Understanding Orchard Joins and Data Relations

查看:137
本文介绍了了解果园连接和数据关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在果园,模块开发人员如何能够了解加入的工作原理,特别是加入核心零件和记录时?我见过的更好的帮助之一是果园文档,但是没有一个示例显示如何与现有或核心部分形成关系。作为我正在寻找的一个例子,这里是一个从一个工作示例中获取的模块服务代码片段:

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

在这种情况下,自定义 TaxonomyPart 正在加入一个核心 RoutePartRecord 。我调查了代码,我看不到一个TaxononmyPart是如何连接到RoutePartRecord。同样,从工作代码来看,这是另一个片段驱动程序代码,它将一个自定义的TagsPart与一个核心CommonPartRecord相关联:

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

我以为我可以从以前的例子中学习如何形成自己的查询。我这样做:

 列表< string> tags = new List< string> { 你好 }; 
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 );

我的代码的意图是将发现的内容项限制在特定容器(或博客)。当代码运行时,它在我的连接查询中抛出异常,说 {无法解析属性:ContentType of:Orchard.Core.Containers.Models.ContainerPartRecord} 。这导致了各种各样的问题:


  1. 为什么在第二个例子的驱动程序的Display()方法中是 CommonPartRecord 填充,但不是 ContainerPartRecord ?一般来说,我如何知道什么部分记录被填充,何时?

  2. 在工作代码片段中,连接的工作原理是因为没有指定连接键/条件(而不是隐式)连接键很明显)例如,我检查了数据迁移文件和模型类,并发现一个TagsPart和CommonPartRecord之间没有固有的关系。因此,除了查看该示例代码之外,任何人都首先知道这样的加入是合法的还是可能的?

  3. 是否在任何上下文中使用 TagsPart ContainerPartRecord ?哪个?

  4. 这些示例的查询语法主要是Orchard,NHibernate或LINQ to NHibernate的反映?如果主要是NHibernate的反映,那么推荐哪个NHibernate书或文章阅读,以便我可以深入到乌节?

关于这些想法和问题的文档似乎有一个漏洞,这使得很难编写一个模块。无论为此主题找到什么答案,我很乐意编译成一篇文章或社区Orchard文档。

解决方案


  1. 连接只在那里启用它后面的位置。这并不意味着被连接的部分实际上将从DB中掉下来。无论最新的1.x源代码如何,将会发生,并且会以1.3的速度发生。

  2. 您不需要条件,因为您只能以这种方式加入零件。连接条件是隐含的:零件由项目ID连接。

  3. 是的。什么是不合法的,在哪里使用的数据是不可用的连接的部分记录。

  4. 这些例子都是Orchard Content Manager查询,所以他们是相当约束,而且只要你不超出他们的界限,就可以很容易地构建,因为这么多可以被假设并将会隐含地发生。如果您需要更多的控制,您可以使用在最新的1.x下降中添加的新HQL功能。

至于孔在文件中,好的,但当然。我们今天所提供的文档只涵盖了平台的一小部分。今天你最好的参考是源代码。您可以对此做出的任何贡献都得到我们和其他社区的高度评价。如果您需要帮助,请通知我。


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.

解决方案

  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.

这篇关于了解果园连接和数据关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆