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

1.8 item create ignores entered data and failes #4476

Open orchardbot opened 10 years ago

orchardbot commented 10 years ago

qt1 created: https://orchard.codeplex.com/workitem/20647

Item create in UI writes the raw record to DB before reading values from the entry form.

This causes problems when default values do not conform to DB - e.g. null values.

This is initial data is later overwritten with entered data and is saved again in the DB.

The fix - as far as I can tell - would be to switch a couple of lines in

File: src\Orchard.Web\Core\Contents\Controllers\AdminController.cs

switch the lines 251 and 253: _contentManager.Create(contentItem, VersionOptions.Draft);

    var model = _contentManager.UpdateEditor(contentItem, this);

fix: first set than save.. var model = _contentManager.UpdateEditor(contentItem, this); _contentManager.Create(contentItem, VersionOptions.Draft);

orchardbot commented 10 years ago

@sebastienros commented:

I am opening it as I want to analyze the impact it can have. This could potentially solve another related issue: https://orchard.codeplex.com/workitem/20574

orchardbot commented 10 years ago

qt1 commented:

BTW, I have tried running the the tests after the fix and it looks like there is no negative impact.

orchardbot commented 10 years ago

@sebastienros commented:

Fixed in changeset c6b6cb1a9897c71c1ba35ed00ae10b9eb3763343

orchardbot commented 10 years ago

qt1 commented:

Thanks!!!

orchardbot commented 10 years ago

@jeffolmstead commented:

Unfortunately while I agree this is an appropriate change, it made the issue discussed here a top priority:

https://orchard.codeplex.com/workitem/20637

As it stands right now (with the above change) when you create a content item that has a field the field data is not persisted (it is just lost). You can return and edit the item to have the field data stored but a permanent solution is needed.

orchardbot commented 10 years ago

qt1 commented:

Hi, Here is some more information that may be useful:

Preparation: Add a FirstName field to User type in migrations like so:

            ContentDefinitionManager.AlterTypeDefinition("User",
                cfg => cfg               .WithPart("User")            );

            ContentDefinitionManager.AlterPartDefinition("User", builder => builder
                .WithField("FirstName", fld => 
                    fld.OfType("TextField")
                    .WithSetting("DisplayName", "First Name")
                    .WithSetting("Attachable", "True")   
            ));
  1. This WORKS: In the UI create a user. The new field appears and when a user is created the new field is persisted correctly.
  2. This FAILS
   UserPart        userPart = _contentManager.New<UserPart>("User");
   // find the field (there must be a better way, this works for me..)
    TextField firstNameField = userPart.ContentItem.Parts.SelectMany(part => part.Fields.Where(field => field.PartFieldDefinition.Name == "FirstName")).Single() as TextField;
     firstNameField.Value = "My first name";
  _contentManager.Create(userPart);

The user item is created properly but the field is not set to "My first name"

  1. This variant WORKS (first create the item then change fields):
   UserPart        userPart = _contentManager.New<UserPart>("User");
  _contentManager.Create(userPart);
    TextField firstNameField = userPart.ContentItem.Parts.SelectMany(part => part.Fields.Where(field => field.PartFieldDefinition.Name == "FirstName")).Single() as TextField;
     firstNameField.Value = "My first name";