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.