从核心数据中获取多个实体的数据

时间:2013-11-16 03:26:33

标签: objective-c core-data

我是 Objective-C 核心数据的新手。作为我学习的一部分,我正在开展一个小项目。我使用ProductOrderItemSalesSupplier等实体创建了核心数据。

属性:

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.

我创建了ProductOrderItem之间的关系,OrderItemProduct相反。

Relationship              Destination         Inverse
ordersItem                 OrderItem           --

for OrderItem to Product:

Relationship              Destination         Inverse
product                   Product              ordersItem

我的主要问题是如何通过加入产品来显示OrderItem中的订购商品: Product-id,ProductName,OrderDate,OrderId,Qty,Price,Total

为了达到上述目的,如果它是正常的sql,我可以像“选择产品ID,产品名称,订单ID,订单日期,数量,价格,总数来自OrderItem o,产品p,其中o.product-id = p.product -id“

使用核心数据从OrderItem获取的方法相同,我可以尝试:

-(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; 
}

我没有从一个实体显示任何问题,但我的问题来自多个实体?

任何帮助或建议都会受到赞赏吗?

我正在尝试在下面的代码中同时添加Product和OrderItems,但我遇到错误“orders.orderToProd = [NSSet setWithObject:entDesc.prodToOrder];”这是正确的方式吗?



- (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 个答案:

答案 0 :(得分:1)

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

返回OrderItem个对象的数组,您可以访问“product”和“order” 简单地使用关系属性,例如:

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

这假设您与 OrderItem 之间的关系product产品 以及从 OrderItem 订单的关系order

请注意 OrderItem 的属性order-id, product-id是多余的, 因为相应的产品订单是通过关系给出的。 (换句话说,Core Data使用关系而不是“外键”。)


对于您添加的问题:假设orderToProd是来自的“一对一”关系 OrdersItem 产品,您可以执行以下操作来建立关系 在新创建的对象之间:

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