0
votes

I have two entities ProductModel and OrderItem. ProductModel has relationship set prodToOrder, inverse orderToProd and one to Many. OrderItem has relationship set with orderToProd with inverse prodToOrder and Many to one.

I have added ProductModel data with out issues but my problem is after i inserted new records to OrderItem then go back and fetch the OrderItem, the product name is null. If i insert both records to ProductModel and OrderItem at the same time it will work but i do not added both entities as the same time i need to pick already existing product name and add the details to OrderItem.

I think the problem is that unless i insert records to both entities at the same time the relationship will not be created.

I Perform the new OrderItem in the below function:

    - (IBAction)cmdSave:(id)sender {


    //Get max productid
    NSNumber *orderitemid =[self getMaxOrderItemID];
    NSNumber *addValue= [NSNumber numberWithInteger:([orderitemid integerValue] + 1) ];
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    OrderItem *orderDetails = [NSEntityDescription insertNewObjectForEntityForName:@"OrderItem" inManagedObjectContext:context];
    ProductModel *prod  =[NSEntityDescription insertNewObjectForEntityForName:@"ProductModel" inManagedObjectContext:context];


    orderDetails.order-id=[NSNumber numberWithInteger:[addValue integerValue]];
    orderDetails.product-id =[NSNumber numberWithInteger:[product-id integerValue] ];
    orderDetails.qty = [NSNumber numberWithInteger:[self.txtQty.text integerValue]];
    orderDetails.price =[NSDecimalNumber numberWithDouble:[self.txtPrice.text doubleValue]]; 
    orderDetails.total = [NSDecimalNumber numberWithDouble:[self.txtTotal.text 
       doubleValue]];

   //if i do the following statement it will insert product id to ProductModel entity but i do not want to do that and how do i maintain relationships without inserting in to ProductModel.


***   prod.product-id=[NSNumber numberWithInteger:[product-id integerValue] ];
   orderDetails.orderToProd=prod;
  ***

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
   }

I appreciate your help or advice on this please.

2
Check your relationships again. Product can have many OrderItems but OrderItem should have only one Product. So something like - Duncan Groenewald

2 Answers

0
votes

You have not grasped the main gist of the object model. You should forget about all the ids. You are using foreign keys to link entities together - that is the wrong way. Relationships exist to eliminate these ids and foreign keys. You set the relationship and you are done.

orderItem.productModel = productModel; 

As you can see, this is very simple and straight forward. Part of your confusion comes from the old database-think left join mentality. If your concrete order item belongs to a certain product model, the relationship should be called "productModel", not "itemToProd". Keep it like simple English whenever you can.

BTW there are few other quirks I would like to point out. When assigning a NSNumber property and you already have a NSNumber from an integer then this is enough:

entity.property = number; 

not

entity.property = [NSNumber numberWithInteger:[number integerValue]];
0
votes

Check your relationships again. Product can have many OrderItems but OrderItem should have only one Product. So something like Product.orderItems is To-Many, while the inverse would be OrderItem.product is To-One.

Then you should be able to create as many Product items you want like so

    ProductModel *prodA  =[NSEntityDescription insertNewObjectForEntityForName:@"ProductModel" inManagedObjectContext:context];
    [prod setValue:@"Product A" forKey:@"name"];
    [prod setValue:[NSNumber numberWithInt:1] forKey:@"productID"];

    ProductModel *prodB  =[NSEntityDescription insertNewObjectForEntityForName:@"ProductModel" inManagedObjectContext:context];
    [prod setValue:@"Product B" forKey:@"name"];
    [prod setValue:[NSNumber numberWithInt:2] forKey:@"productID"];

etc...

Now to create the OrderItem do the following:

    OrderItem *orderItem1  =[NSEntityDescription insertNewObjectForEntityForName:@"OrderItem" inManagedObjectContext:context];
    [orderItem1 setValue:prodA forKey:@"product"];
    [orderItem1 setValue:[NSNumber numberWithInt:10] forKey:@"quantity"];

etc...

It is not necessary to use the productID to link OrderItem and Product. The OrderItem.product attribute points directly to the Product object.

Now save changes - at this point the model will be validated so you must have all the relationships correct or it will fail with a validation error.

[context save];

To get the list of all OrderItems for a Product just use prodA.orderItems which returns an NSSet of OrderItems.