📜 ⬆️ ⬇️

Orchard CMS: software content of the site

Imagine a situation: it is necessary to transfer a large amount of some data to a site made on Orchard. For example, it may be an online store, site directory, etc.

Suppose we need to create a website - a catalog of cars. For example, let's agree that for each car you need to store the model name, price, year of manufacture and the number of available cars. Using the Orchard source code and a considerable number of articles on the topic of writing modules, we will create our own ContentType.

Model - Car class with properties Model, Price, Year, Quantity:

public class CarPartRecord : ContentPartRecord { public virtual string Model { get; set; } public virtual decimal Price { get; set; } public virtual int Year { get; set; } public virtual int Quantity { get; set; } } public class CarPart : ContentPart<CarPartRecord> { public string Model { get { return Record.Model; } set { Record.Model = value; } } public decimal Price { get { return Record.Price; } set { Record.Price = value; } } public int Year { get { return Record.Year; } set { Record.Year = value; } } public int Quantity { get { return Record.Quantity; } set { Record.Quantity= value; } } 

')
View:

  @T("Model: ") @Model.Model<br /> @T("Price: ") @Model.Price<br /> @T("Year: ") @Model.Year<br /> @T("Quantity: ") @Model.Quantity<br /> 


Well, let's say the content type is created. The main problem is that the car catalog we need needs to be imported into Orchard. How to do it?

Obviously, if the data volume exceeds 50 records, I would like to add them to the CMS programmatically. In Orchard, in order to write data through the database, it is necessary for each element to add records to 5 related tables:



Then there are two options: use either SQL scripts or LINQtoSQL. Personally, when I checked the first option, I could not do without the construction of the form

 insert ... select ... 

with a large number of records (checked for 3000+), it leads to a completely unacceptable operating time (about a few hours).

Therefore, we will focus on the second option: we will write a simple WinForms application, which we will import data.

When you start the import, 5 functions should be sequentially called that add entries to each of the above tables, for example:

 private CarPartRecord create(/*   CSV-, XMLNode       */) { CarPartRecord record = new CarPartRecord(); record.Model = // record.Price = //   ContentPart record.Year = //  record.Quantity = // return record; } private Orchard_Framework_ContentItemRecord related1_create() { Orchard_Framework_ContentItemRecord record = new Orchard_Framework_ContentItemRecord(); record.ContentType_id = 9; //    "" CMS  8  ContentType,     return record; } private Orchard_Framework_ContentItemVersionRecord related2_create(CarPartRecord item) { Orchard_Framework_ContentItemVersionRecord record = new Orchard_Framework_ContentItemVersionRecord(); record.Number = 1; record.Latest = true; record.Published = true; record.ContentItemRecord_id = item.Id; return record; } private Common_CommonPartRecord related3_create(int id) //  ID  CarPartRecord { Common_CommonPartRecord record = new Common_CommonPartRecord(); record.CreatedUtc = DateTime.Now; record.ModifiedUtc = DateTime.Now; record.PublishedUtc = DateTime.Now; record.OwnerId = 2; record.Id = id; return record; } private Common_CommonPartVersionRecord related4_create(int id) //  ID  CarPartRecord { Common_CommonPartVersionRecord record = new Common_CommonPartVersionRecord(); record.CreatedUtc = DateTime.Now; record.ModifiedUtc = DateTime.Now; record.PublishedUtc = DateTime.Now; record.ContentItemRecord_id = id; record.Id = id; return record; } 


And, accordingly, in main:

 CarPartRecord record = create(/*   */); context.CarPartRecords.InsertOnSubmit(record); //     context.SubmitChanges(); Orchard_Framework_ContentItemRecord tmp1 = related1_create(); //  ,    ID  CarPartRecord. context.Orchard_Framework_ContentItemRecords.InsertOnSubmit(tmp1); Orchard_Framework_ContentItemVersionRecord tmp2 = related2_create(record); context.Orchard_Framework_ContentItemVersionRecords.InsertOnSubmit(tmp2); Common_CommonPartRecord tmp3 = related3_create(record.Id); context.Common_CommonPartRecords.InsertOnSubmit(tmp3); Common_CommonPartVersionRecord tmp4 = related4_create(record.Id); context.Common_CommonPartVersionRecords.InsertOnSubmit(tmp4); context.SubmitChanges(); 


As a result, the import of 3000+ entries will last about 100 seconds on a medium configuration machine. Profit

Source: https://habr.com/ru/post/170759/


All Articles