Orchard CMS: software site content
- From the sandbox
- Tutorial
Imagine a situation: you need to transfer to a site made on Orchard, a large amount of some data. For example, it can be an online store, a catalog site, etc.
Let's say we need to create a website - a catalog of cars. For example, we agree that for each car you need to store the model name, price, year of manufacture and the number of available cars. Using the sources of Orchard and a considerable number of articles on the topic of writing modules, create your own ContentType.
Model - Car class with the properties Model, Price, Year, Quantity:
View:
Well, let's say the content type is created. The main problem is that we need to import the car catalog we need into Orchard. How to do it?
Obviously, with data over 50 records, I would like to programmatically add them to the CMS. In Orchard, in order to write data through the database, it is necessary to add records for each element in 5 related tables:
Two options are possible further: use either SQL scripts or LINQtoSQL. Personally, when I checked the first option, I could not do without a design of the form
which with a large number of records (checked for 3000+) leads to a completely unacceptable working time (of the order of several hours).
Therefore, we will focus on the second option: we will write a simple WinForms application, which we will import data from.
When starting the import, 5 functions must be called sequentially that add entries to each of the above tables, for example:
And, accordingly, in main:
As a result, import of 3000+ records will last about 100 seconds on a medium configuration machine. Profit
Let's say we need to create a website - a catalog of cars. For example, we agree that for each car you need to store the model name, price, year of manufacture and the number of available cars. Using the sources of Orchard and a considerable number of articles on the topic of writing modules, create your own ContentType.
Model - Car class with the 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
{
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
@T("Price: ") @Model.Price
@T("Year: ") @Model.Year
@T("Quantity: ") @Model.Quantity
Well, let's say the content type is created. The main problem is that we need to import the car catalog we need into Orchard. How to do it?
Obviously, with data over 50 records, I would like to programmatically add them to the CMS. In Orchard, in order to write data through the database, it is necessary to add records for each element in 5 related tables:
- ModuleName_CarPartRecord
- Common_CommonPartRecord
- Common_CommonPartVersionRecord
- Orchard_Framework_ContentItemRecord
- Orchard_Framework_ContentItemVersionRecord
Two options are possible further: use either SQL scripts or LINQtoSQL. Personally, when I checked the first option, I could not do without a design of the form
insert ... select ...
which with a large number of records (checked for 3000+) leads to a completely unacceptable working time (of the order of several hours).
Therefore, we will focus on the second option: we will write a simple WinForms application, which we will import data from.
When starting the import, 5 functions must be called sequentially 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, import of 3000+ records will last about 100 seconds on a medium configuration machine. Profit