OrchardCMS / Orchard

Orchard is a free, open source, community-focused Content Management System built on the ASP.NET MVC platform.
https://orchardproject.net
BSD 3-Clause "New" or "Revised" License
2.38k stars 1.12k forks source link

Save content item with ContentPicker field by ContentManager New method #7598

Open ziic opened 7 years ago

ziic commented 7 years ago

I observe some strange/magic behaviour (at least since version 1.8). But only now I decided to share. Maybe I do something wrong.

Brief: If a content item of Content type with ContentPickerField is created by New method of ContentManger, later a field value is populated and next saved by Create (and/or Publish) then the field value is empty. If use immediately Create (with/without Publish) - it works correctly.

Detailed how to reproduce:

  1. Create a new ContentType. Attach ContentPickerField (I did in admin-panel). Content type definition screenshot
  2. In next examples the value of ContentPickerField is saved.
    
    var temp = _contentManager.Create("Temp");
    ContentPickerField field = temp.Content.Temp.ContentPicker;
    field.Ids = new[] { ExistingPageId };
    //request end, transaction silently committed
    //OR
    var temp = _contentManager.Create("Temp", VersionOptions.Draft);
    ContentPickerField field = temp.Content.Temp.ContentPicker;
    field.Ids = new[] { ExistingPageId };
    _contentManager.Publish(temp);
    //request end, transaction silently committed
    //OR 
    var temp = _contentManager.Create("Temp", VersionOptions.Published);
    ContentPickerField field = temp.Content.Temp.ContentPicker;
    field.Ids = new[] { ExistingPageId };
    //request end, transaction silently committed
3. In next examples the value of ContentPickerField **is note saved**
```C#
var temp = _contentManager.New("Temp");
ContentPickerField field = temp.Content.Temp.ContentPicker;
field.Ids = new[] { ExistingPageId };
_contentManager.Create(temp);
//request end, transaction silently committed
//OR
_contentManager.Create(temp, VersionOptions.Draft);
_contentManager.Publish(temp);
//request end, transaction silently committed
//OR
_contentManager.Create(temp, VersionOptions.Published);
//request end, transaction silently committed
  1. If trace in debug I see next: Inside public virtual void Create(ContentItem contentItem, VersionOptions options) method of DefaultContentManager, the value of ContentPickerField is removed after ContentItemVersionRecord is created and attached to content item. Debug Before VersionRecord Created screenshot Debug After VersionRecord Created screenshot
sebastienros commented 7 years ago

Does it only happen with this field? Have you tried a simple TextField?

ziic commented 7 years ago

Recently I've tried and it does NOT work correctly with a TextField too.

in my projects I usually fix it just with:

           var contentItem = ContentManager.New(ContentType);           
           // produce root record to determine the model id
           contentItem.VersionRecord = new ContentItemVersionRecord
           {
                    ContentItemRecord = new ContentItemRecord(),
                    Number = 1,
                    Latest = true,
                    Published = true
           };
sebastienros commented 7 years ago

Thanks, that's important to know, we can now check the unit tests and see if it's by design or add a new one.