How to delete version records of a content item in Orchard?

送分小仙女□ 提交于 2020-01-24 10:19:12

问题


I have a Orchard CMS running that is tied to a user synchronization. This sync updates each user overnight and the code begins with

... = mContentManager.Get<Orchard.Users.Models.UserPart>(lOrchardUser.ContentItem.Id,
  Orchard.ContentManagement.VersionOptions.DraftRequired);

As you can see VersionOptions.DraftRequired is passed to the Get() method with creates a new draft each time the user is synchronized. It's not intended to create a new draft here so i changed it to VersionOptions.Published which avoids creating a new version record on each call.

But issue here is that passing VersionOptions.DraftRequired in the past has created around 120 version records for each user whereas there are around 1000 users in the DB.

When i now use IContentManager.Query() it takes considerably longer due to the high amount of versions.

My idea is to remove all versions except the published one as i don't need them but IContentManager doesn't provide any version removal option and deleting the records by using IRepository<> causes a NHibernate exception.

So my last resort was to use LINQ queries to remove the versions, this did work but i was told that using LINQ in Orchard is not recommended.

I wonder whether anyone had any issues with high amounts of version records as the longer the system runs the more data is accumulated and the system is getting slower and slower.

So, my questions are

  1. is there an Orchard way to remove version records?
  2. is there a way to disable versioning?

回答1:


As there seems no Orchard way to do this, i deleted the versions with the following LINQ 2 SQL code:

  public System.Web.Mvc.ActionResult RemoveVersions()
  {
    // select user ids, an alternate way is to retrieve user IDs via content manager but this takes a veeeeery long time due
    // to the need of querying all versions
    //
    // var lOrchardUserIDs = mContentManager
    //  .Query<Orchard.Users.Models.UserPart, Orchard.Users.Models.UserPartRecord>(Orchard.ContentManagement.VersionOptions.AllVersions)
    //  .List()
    //  .Select(u => u.Id)
    //  .ToList()
    var lOrchardUserIDs = mUserRepository.Fetch(u => true).Select(u => u.Id).ToList();

    foreach (var lOrchardUserID in lOrchardUserIDs)
    {
      var lContentItemVersionRecords =
        (from r in DataContext.ContentItemVersionRecords where r.ContentItemRecord_id == lOrchardUserID select r).ToList();

      if (lContentItemVersionRecords.Count > 1)
      {
        foreach (var lContentItemVersionRecord in lContentItemVersionRecords)
        {
          if (lContentItemVersionRecord.Number == 1)
          {
            if (lContentItemVersionRecords[lContentItemVersionRecords.Count - 1].Published)
            {
              lContentItemVersionRecord.Latest = true;
              lContentItemVersionRecord.Published = true;
            }
          }
          else
            DataContext.ContentItemVersionRecords.DeleteOnSubmit(lContentItemVersionRecord);
        }
      }
    }

    DataContext.SubmitChanges();

    return Content("Done");
  }


来源:https://stackoverflow.com/questions/35486133/how-to-delete-version-records-of-a-content-item-in-orchard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!