Is there a way to Clone one or many Entities (records) in Code

不羁的心 提交于 2021-02-11 13:12:36

问题


NOTE: at this time I am stuck on 2sxc v9.43.2 on this project.

After selecting a set of records from my Content Type, I need to be able to duplicate them changing 1 of the fields along the way. Here is my almost-working idea so far. The use case is simple, they have Programs that people can register for. They change each Season, but only a little (prices, dates/times, etc). And they need the Current Season live and unchanged while they edit the Next Season. So we are still in the fall season (EntityId 1732) with 97 active programs. We want to click a button and clone all 97 programs as is, but IN TO the new Next Season (1735 below).

Two questions:

  1. if this way works, what syntax would work on ent/Attributes to delivery the "object" as needed in the fields.Add() line
  2. is there another 2sxc way to do this? Some other variant of the App.Data.Create() method or some other method in the API? I just need to duplicate the record with 1 field (Season) changed?
  3. is there a better way to do this in the latest versions of 2sxc, v11.7+ ?
    // we are going to duplicate the current Season's programs in to the new season
    // cheating for now, pre-made new 1735 in Seasons, current is 1732
    var programs = AsDynamic(App.Data["Programs"])
      .Where(p => ((List<DynamicEntity>)p.Season).First().EntityId == selectedSeason.EntityId);
    // @programs.Count() // 97
    foreach(var copy in programs)
    {
      var fields = new Dictionary<string, object>();
      var ent = AsEntity(copy);
      foreach(var attr in ent.Attributes)
      {
        if(attr.Key == "Season")
        {
          fields.Add(attr.Key, new List<int> { 1735 });
        }
        else
        {
          fields.Add(attr.Key, ent.GetBestValue(attr.Key));   // object??
        }
      }
      App.Data.Create("Programs", fields);
    }

回答1:


There are at least 3 ways to clone

  1. Simple way using edit-ui
  2. hard way using c# / server api
  3. Semi-hard way using REST api

The simple way is to use the edit ui. You can see an example in the replace-dialog, there is a copy button there. This would open the edit UI with an existing item, but tell it it's a copy, so on save it would create a new one.

Combine this with a prefill or something and I think you would be good to go.

The second way is using the App.Data.Create - your code looks fairly good. I assume it also works and you were just wondering if there was a 1-liner - or am I mistaken?

The last way is using JS REST. Basically write some JS that gets an item, changes the object (resets the id) and posts it back to the endpoint for saving.




回答2:


Just stumbled upon situation where I needed to create entity and set its field value, which has type of another entity. If that's your question #1, you need to add there EntityGuid. fields.Add(attr.Key, attr.EntityGuid);. That should bind one entity to another one. And no, I didn't stumble upon better way to copy entity then just to create a new one. At least so far.



来源:https://stackoverflow.com/questions/64762167/is-there-a-way-to-clone-one-or-many-entities-records-in-code

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