0
votes

I am new to Objective-C and Core Data. I am working on a small project as part of my learning. I created Core Data with entities such as Product, OrderItem, Sales and Supplier.

Attributes:

Product : product-id, supplier-id, productName, category, subcategory,qty, price,minStock,frequency.
Order : order-id, orderDate.
OrderItem : order-id, product-id,qty,price, total, orderDate.

I have created relationships between Product and OrderItem with OrderItem inverse to Product.

Relationship              Destination         Inverse
ordersItem                 OrderItem           --

for OrderItem to Product:

Relationship              Destination         Inverse
product                   Product              ordersItem

My main issue is how do I display ordered items from OrderItem by joining product as: Product-id, ProductName, OrderDate, OrderId, Qty, Price, Total ?

To achieve above if it is normal sql i could run like "Select product-id, productName, order-id, orderDate, qty, price, total from OrderItem o, Product p where o.product-id=p.product-id"

The same way to fetch from OrderItem using core data i could try:

-(NSMutableArray *)loadData{

 // Fetch the OrderItems from OrderItem model through from persistent data store
NSManagedObjectContext *managedObjectContext = [self context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"OrderItem"];

//How do i do to get productName from Product
//the relationship is based on "product" from OrderItem to Product
//I need to get productName and add to array list below to display on the screen.

NSMutableArray *orderList =[[NSMutableArray alloc]init];

orderList =[[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

return orderList; 
}

I do not have any issue displaying from one entity but my issue is from multiple entities?

Any help or advice would be appreciated?

I am trying to add Product and OrderItems at the same time in the code below but i am having error "orders.orderToProd = [NSSet setWithObject: entDesc.prodToOrder];" is it the right way ?



- (IBAction)cmdAdd:(id)sender {

    NSManagedObjectContext *context =[self managedObjectContext];
    Product *entDesc =[NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:context];

    //set Product details
    entDesc.productName = self.txtName.text;
    entDesc.category = self.txtCategory.text;
    entDesc.qty=[NSNumber numberWithInteger:[self.txtQty.text integerValue]];

    //add OrderItems details here
    OrderItems *orders=[NSEntityDescription insertNewObjectForEntityForName:@"OrderItems" inManagedObjectContext:context];
    orders.price=[NSNumber numberWithInteger:[self.txtprice.text integerValue]];
    orders.orderQty=[NSNumber numberWithInteger:[self.txtOrderQty.text integerValue]];
    orders.total=[NSNumber numberWithInteger:[self.txtTotal.text integerValue]];

    //orderToProd is the relationship object from orderItem to Product and prodToOrder is relationship from Product to OrderItem

    //I am getting error with this line.  I think it expects for array or comma based items
     //Please help
     orders.orderToProd = [NSSet setWithObject: entDesc.prodToOrder];


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


}

1

1 Answers

1
votes
NSArray *orderItems = [managedObjectContext executeFetchRequest:fetchRequest error:nil];

returns an array of OrderItem objects, and you can access the "product" and "order" attributes simply using the relationships, for example:

for (OrderItem *oi in orderItems) {
    Product *product = oi.product;
    Order *order = oi.order;
    // Display product.productName, oi.qty, order.order-id, ...
}

This assumes that you have a relationship product from OrderItem to Product and also a relationship order from OrderItem to Order.

Note that the attributes order-id, product-id of OrderItem are redundant, as the corresponding Product and Order is given via the relationships. (In other words, Core Data uses relationships and not "foreign keys".)


To your added question: Assuming that orderToProd is a "to-one" relationship from OrdersItem to Product, you would do the following to establish a relationship between the newly created objects:

Product *entDesc = ...
// ...
OrderItems *orders = ...;
// ...
orders.orderToProd = entDesc;