Orchard CMS:将默认数据添加到字段,然后查询它们 [英] Orchard CMS: Adding default data to fields and then querying them

查看:102
本文介绍了Orchard CMS:将默认数据添加到字段,然后查询它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与内容类型同名的部件在内容类型中添加了一个名为Website的LinkField.

I have added a LinkField called Website to a content type using a part with the same name as the content type.

ContentDefinitionManager.AlterTypeDefinition("MyContentType", a => a
    .WithPart("CommonPart")
    .WithPart("MyContentType")
    .Creatable());

ContentDefinitionManager.AlterPartDefinition("MyContentType", cft => cft
    .WithField("Website", a => a.OfType("LinkField").WithDisplayName("Website")
        .WithSetting("FieldIndexing.Included", "True"))
    .Attachable());

然后在迁移过程中创建一些默认内容项.
我在添加字段数据之前创建了该项目,因为在创建项目之前设置了它们的值时,我遇到了字段无法更新的问题. (可以对此进行一些说明,但这不是我的问题)

I then create some default content items during the migration.
I'm creating the item before adding the field data because I have had problems with fields not being updated when their values are set before the item is created. (Feel free to shine some light on that, but that isn't my question though)

var myItem = _orchardServices.ContentManager.New("MyContentType");
_orchardServices.ContentManager.Create(myItem);
var websitePart = myItem.Parts.FirstOrDefault(x => x.Fields.Any(y => y.Name == "Website"));
var websiteLinkField = websitePart .Fields.FirstOrDefault(x => x.Name == "Website") as LinkField;
websiteLinkField.Value = "http://www.google.com";
websiteLinkField.Text = "Link to google";
_orchardServices.ContentManager.Publish(myItem);

我知道有更多动态方法可以访问该字段,但这似乎也可行. 当我查看项目时,数据就会显示出来,但随后我继续进行查询. 我使用UI构建一个简单的查询,在LinkField的文本中查找单词"google",然后单击预览".
找不到结果. 然后,我打开从迁移创建的项目之一,然后单击保存"按钮. 然后,我再次尝试预览,现在显示我保存的项目.

I realize there are more dynamic ways to access the field, but this seems to work too. The data shows up when I view the items, but then I move on to making a Query. I use the UI to build a simple query looking for the word "google" in the text of the LinkField, then I hit preview.
No results are found. I then open up one of the items created from the migration and simply hit the "Save" button. Then I try the preview again and the item I saved now shows up.

据我所知,当我保存迁移中没有使用的内容项时,正在发生某些事情.但是我一直在逐步遍历所有角度的代码,但我找不到它. 我怀疑也许应该为了创建FieldIndex's而触发某些处理程序? (我知道如何触发Lucene索引的更新,但是正如人们所期望的那样,它不会影响使用Projections模块进行的字段查询,这时我真的迷路了.)

So as far as I can tell something is happening when I save a content item that I'm not doing from the migration. But I have been stepping through the code going over all angles, and I just can't find it. I suspect maybe some handler is supposed to get triggered in order to create the FieldIndex'es ? (I know how to trigger an update for the Lucene index, but as one would expect it does not affect querying fields using the Projections module and I'm really lost at this point.)

现在我只是在黑暗中盲目刺伤.
我真的很感激我能获得的任何帮助,即使这只是指向我正确方向的东西.谢谢.

By now I'm just stabbing blindly in the dark.
Would really appreciate any help I can get, even if it's just something pointing me back in the right direction. Thank you.

推荐答案

您应该更改

_orchardServices.ContentManager.Create(myItem);

_orchardServices.ContentManager.Create(myItem, Orchard.ContentManagement.VersionOptions.Draft);

为了理解,请查看Orchard.Core.Contents.Controllers.AdminController类的CreatePOST方法和Orchard.ContentManagement.DefaultContentManager类的Publish方法

For understanding look at CreatePOST method of Orchard.Core.Contents.Controllers.AdminController class and Publish method of Orchard.ContentManagement.DefaultContentManager class

在您的情况下,当您调用Create(myItem)然后创建了已发布的内容项,并且所有处理程序都被正常调用(但尚未设置所需的数据)时.当您调用Publish(myItem)时,不会发生任何事情(不会调用任何处理程序),因为您的内容已经发布.

In your case when you call a Create(myItem) then created published content item and all handlers are invoked normally (but has not yet set up a desired data). When you call Publish(myItem) nothing happens (no handlers are invoked) because your content is already published.

这篇关于Orchard CMS:将默认数据添加到字段,然后查询它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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