I need help in adding extra product fields in database.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hi

thank you SOOO very much for this tutorial, you're the best:

I have few questions:

Just because 1.9v is out so now i am trying to update it to 1.9v and you mentioned that:

"3. This example is specifically for nopCommerce v1.8.  I believe it will work for 1.7 as well.  < 1.7 uses the ADO.Net convention.  For 1.9 the process is similar but instead of manager classes you will need to implement interfaces instead."

What exactly you mean by interfaces ? how to go about it ?


Like i said in my 1st post: I already have a table added to database "ProductVariant_AdditionalFields (which I added in 1.4v) So when I upgrade the database the table remains there in my upgraded 1.9v which is linked with products (When i upgrade the database i don't want to delete the table because it contains data)

So how to go about it ? Because you mentioned " Tables that have relationships adds another wrinkle to this process.  It is not difficult but at this time, outside the scope of this tutorial. "

thankyou
super
13 years ago
Super,

You mentioned in a PM that you are still having problems.  We need to break this down and figure out what you have done and what you haven't done.

Before we proceed I think you should you look closely at what your needs are for your customization.  If your only need is to add MSRP and UPC fields to a product variant then I would just add those columns directly to the product variant table and then update that class and any methods where you need to pass those variables in individually (as opposed to passing in a ProductVariant object).  This is much easier to implement and won't get in the way of any future upgrades because there won't be any for the web forms version of nopCommerce (1.9 has no feature enhancements and 2.0 will be MVC).  If this is not your case and you have a good reason to add a custom table then proceed.

Let's stick with nop 1.8 for now because I don't want to introduce any  new concepts until we get your implementation working.

1. Have you tried starting over and following my example I posted.  I would suggest doing it this way because you could have introduced some code somewhere that is causing problems.

2. Based on your table screen shot it appears you are mapping ProductVariantId and ProductID to your custom table.  There are a couple of issues I see based on the code you posted.

a. Your table does not use a primary key (Id).  It isn't always necessary but you can introduce issues by not using one.  I would add one.

b. Your table column "ProductVariantId" does not match your property of "ProductVariantID" in your class file.  As I stated in my previous post, all names must match EXACTLY.

c. You stated that you created a folder in Nop.BusinessLogic called extensions.  What did you put in there?  Your class file based on your namespace is located Nop.BusinessLogic.Products.  Keep things simple and don't bother adding any new folders.

d. In your db table you have a column called "COST" yet there is no reference to this property in your class file or your static insert method.

e. Your insert method looks troubling.  

1. What is the object "DBProductVariantAddInfo"?  Your class name is ProductVariantAddInfo.
2. I am not familiar with DBProviderManager.  I searched 1.8 and 1.9 and there is no reference to this.  
3. I think you have custom code from someone else that you are trying to introduce into 1.8.  Are you sure you copied over all of the previous developers files?

3. You stated that you are getting errors all over the place.  Try step #1 and then post what error you are getting and we can knock them out one by one.  "Errors all over the place" is a bit nebulous.

Finally, if your implementation is as simple as you have posted, I STRONGLY recommend my first option which is what aGorrilla posted as well.  It is so much simpler and it won't effect future upgrades.

Good luck.

t
13 years ago
super wrote:

super

have you updated  your database to 1.9v ?

Hey Trevor,
I have sent you a PM?
13 years ago
joebloe wrote:
Super,

You mentioned in a PM that you are still having problems.  We need to break this down and figure out what you have done and what you haven't done.

Before we proceed I think you should you look closely at what your needs are for your customization.  If your only need is to add MSRP and UPC fields to a product variant then I would just add those columns directly to the product variant table and then update that class and any methods where you need to pass those variables in individually (as opposed to passing in a ProductVariant object).  This is much easier to implement and won't get in the way of any future upgrades because there won't be any for the web forms version of nopCommerce (1.9 has no feature enhancements and 2.0 will be MVC).  If this is not your case and you have a good reason to add a custom table then proceed.
t


Hi Trevor,

first i want to thank you for helping me with this issue and taking out time for me.

I have tried so many things but don't know where am i going wrong.

Yes my friend helped me with 1.4v so i was trying to use that code in 1.9v but I know it won't work because 1.9 is entity based which is the issue because i am not very familiar with entity based. I can follow the steps if anyone can guide me with this issue.

My question is, as my extra table which is "ProductVariant_AdditionalFields" is not linked with any other table and used to update product information in 1.4v on the bases of written code.

So when I updated my database from 1.4 to 1.9, I didn't face any problem because the table was not connected with any other table so now as i have updated my database to 1.9v, the "ProductVariant_AdditionalFields" table remains there in the database along with it, I didn't encounter any issues while updating.

My thinking is that if I add MSRP and UPC in the default nopcommerce table "ProductVariant" then in future if i will update the database to 2.0, I will face some problem because MSRP and UPC extra columns in my ProductVariant table and the update script won't have these fields so i might get errors/problem while updating, am i correct or no ? please correct me if iam wrong.

I think if i would have linked my extra table "ProductVariant_AdditionalFields" in 1.4v with other table then i would have encountered some errors while updating the database to 1.9v because for update i think it is recommended to undo  the database changes and then do the upgrade. Again, Plz correct me if my thinking or logic is wrong ?

Yes, even I want to keep everything simple so that in future when i will upgrade i don't face any problem since nopcommerce is going to be MVP based from 2.0v.

I have already updated my database to 1.9v so i would deeply appreciate if you could help me with the process in such a way that in future upgrades i don't face any problem.

thanks
super
13 years ago
Hi Super,

I understand your situation and have a good idea what has transpired.  Unfortunately I don't have a lot of time right now to go over this.  Here are a couple of quick suggestions.

Do you still have a backup of your 1.4 database?

If you do, then I would upgrade it to 1.8 and stop.  There are NO feature enhancements from 1.8 to 1.9.  2.0 will be on the MVC framework and I would anticipate a little bugginess in some of the early releases.  I have been reviewing the code base for 2.0 so far and it does not appear (yet) that there will be a lot of feature enhancements sans a new layout or admin interface.

My point is that if you want to quickly solve your problem then use my instructions for 1.8 and rest easy.  If you have your heart set on 1.9 then it may take more time.  I have only briefly looked at 1.9 so I can't say how much will be different.

Sorry I can't give you a better answer now.

Regards,

Trevor
13 years ago
Trevor,  Thank you very much for your very informative tutorial.  It was of great help in understanding the nopCommerce design.  I am wondering could you explain how to do this for 1.9 ?  There is no previous version of the DB.  I just want to add tables to the database and use them in nopCommerce 1.9.

I would have to create a new table Nop_Widget off the Nop_ProductVariant table with a 1:* relationship.  Should I build this relationship in the database or should I just add it to the edmx?   Do we just create the Widget class and implement the interface in VS?  If you could modify the step-by-step 1.8 tutorial above for 1.9,  I and I think many others would greatly appreciated it.
13 years ago
Let me see what I can do. Busy on another project
13 years ago
chorse wrote:
If you could modify the step-by-step 1.8 tutorial above for 1.9,  I and I think many others would greatly appreciated it.


Ditto that.

Also, will this process change significantly when 2.0 comes out?

And, is it generally agreed that to avoid problems with future upgrades, modifying the nop tables should be avoided. I.e., better to add new tables?

Thx!

Steven
13 years ago
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
13 years ago
@joebloe - That is extremely helpful Thank you.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.