Hello All,
A few posters have requested a detailed step-by-step process for adding tables to 1.9. For those of you who are just reading this post, I STRONGLY encourage you to read the entire thread. I plan on referring to my previous post as the steps for 1.9 are not much different than 1.8.
I would love to be able to provide a full blown solution but I just don't have the time, so I will condense my example into a more conceptual description that a detailed one.
User 'chorse' is looking to add a conference product to his shopping cart. The logic he is requesting is as follows.
1. Customer finds conference he or she wants to attend and adds the conference to the cart.
2. The customer must now add any other attendees who wish to attend the conference as well.
3. These additional attendees names and addresses must be stored in the database for other uses.
4. All of this functionality must be on the product description page and selectable like any other product attribute. i.e color/size.
The achieve the desired result, there are basically 3 steps.
1. Design and implement the custom db tables necessary to achieve the desired functionality.
2. Design and implement the custom logic necessary to gather and record the user input.
3. Design and implement the ui that allows the user to interact with the shopping cart.
THIS EXAMPLE WILL ONLY FOCUS ON #1 AND IS ONLY A SIMPLE EXAMPLE.
The steps for this are almost exactly the same as my previous example for 1.8. Please refer to that example for detailed code examples.
1. Create the tables in the db.
For this example (if we don't want to alter the existing tables) we could add 2 tables. Attendee & AttendeeProductVariant. This will allow us to map are attendees that the customer will enter to the product variant that they are purchasing (the conference). Make sure you add your mapping and foreign key constraints.
2. Create the class files for the new tables.
This is where the previous example differs slightly. Since our 2 tables have a 1 to many relationship then we need to setup navigation properties. Secondly, 1.9 uses Unity IoC. I won't go into an explanation of what this is or how it works as there are far better explanations out there than what I have time to explain. Suffice it say, you need to make sure you register your custom properties to use your service layer. Here is an example.
public partial class Attendee
{
#region Properties
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
#endregion
#region Navigation Properties
//This is our 'incoming' navigation property if you will
public virtual ICollection<Attendee_ProductVariant_Mapping> NpAttendee_ProductVariant_Mapping { get; set; }
#endregion
}
public partial class Attendee_ProductVariant_Mapping
{
#region Properties
public int Id { get; set; }
public int AttendeeId { get; set; }
public int ProductVariantId { get; set; }
#endregion
//This area registers our mapping properties to our service classes through the Ioc
#region Custom Properties
public Attendee Attendee
{
get { return IoC.Resolve<IAttendeeService>().GetAttendeeById(this.AttendeeId); }
}
public ProductVariant ProductVariant
{
get { return IoC.Resolve<IProductService>().GetProductVariantById(this.ProductVariantId); }
}
#endregion
//This area is our 'outgoing' properties if you will
#region Navigation Properties
public virtual Attendee NpAttendee { get; set; }
public virtual ProductVariant NpProductVariant { get; set; }
#endregion
}
3. Update the DB Schema
This step is no different than my previous example.
3. Update Object Context
You can skip this step as 1.9 uses auto generated .tt classes.
5. Create new Service classes.
This is also similar to 1.8 except that Interface classes are used instead. And it is not necessary to call an instance of the ObjectContext everytime. This is injected and referenced in your constructor. So if you have an Attendee Service class like this.
public partial class AttendeeService : IAttendeeService
{
private readonly NopObjectContext _objectContext;
public AttendeeService(NopObjectContext objectContext)
{
this._objectContext = objectContext;
//I omitted the cache manager which you are free to implement if you want.
}
public List<Attendee> GetAllAttendees()
{
var query = from a in _objectContext.Attendees
//If you don't see your custom class popup in intellisense then you need to retrace your steps
select a;
var attendees = query.ToList();
return attendees;
}
}
Then your interface class would look like this.
public partial interface IAttendeeService
{
List<Attendee> GetAllAttendees();
}
Build your project and you have successfully added custom tables to 1.9.
Master steps 2 and 3 from the top of this post are what you would work on next. I may have time in the future to go over these.
Hope this helps some people and let me know if you see any errors. This is just off the top of my head.
trevor