从核心数据获取多个实体的数据 [英] Fetch data from core data multiple entities
问题描述
我是 Objective-C 和核心数据的新用户。我正在做一个小项目作为我的学习的一部分。我使用产品
, OrderItem
,销售
和供应商
。
产品:product- id,supplier-id,productName,category,subcategory,qty,price,minStock,frequency。
订单:order-id,orderDate。
OrderItem:order-id,product-id,qty,price,total,orderDate。
我已经创建了产品
和 OrderItem
with OrderItem
与产品
相反。
关系目标反向
ordersItem OrderItem -
p>
关系目标反向
产品订单Item
我的主要问题是如何通过加入产品显示OrderItem中的有序项目:
Product-id,ProductName,OrderDate,OrderId,Qty,Price,Total
如果是正常的sql,我可以像Select product-id,productName,order-id,orderDate,qty,price,total from OrderItem o,Product p .product-id = p.product-id
使用核心数据从OrderItem获取的方法相同,可以尝试:
- (NSMutableArray *)loadData {
//通过持久数据存储从OrderItem模型获取OrderItems
NSManagedObjectContext * managedObjectContext = [self context];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@OrderItem];
//如何从产品中获取productName
//关系是基于OrderItem到Product的product
//我需要获取productName和add到下面的数组列表显示在屏幕上。
NSMutableArray * orderList = [[NSMutableArray alloc] init];
orderList = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
return orderList;
}
我没有从一个实体显示任何问题,
我试图在产品和订单项中添加产品和订单项同时在下面的代码,但我有错误orders.orderToProd = [NSSet setWithObject:entDesc.prodToOrder];是正确的方式吗?
- (IBAction)cmdAdd:(id)sender {
NSManagedObjectContext * context = [self managedObjectContext];
Product * entDesc = [NSEntityDescription insertNewObjectForEntityForName:@ProductinManagedObjectContext:context];
// set产品详细信息
entDesc.productName = self.txtName.text;
entDesc.category = self.txtCategory.text;
entDesc.qty = [NSNumber numberWithInteger:[self.txtQty.text integerValue]];
//这里添加OrderItems的细节
OrderItems * orders = [NSEntityDescription insertNewObjectForEntityForName:@OrderItemsinManagedObjectContext: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是从orderItem到Product的关系对象,prodToOrder是从Product到OrderItem的关系
//我收到此行的错误。我想它期望基于数组或逗号的项目
//请帮助
orders.orderToProd = [NSSet setWithObject:entDesc.prodToOrder];
NSError * error = nil;
//将对象保存到持久存储
if(![context save:&error]){
NSLog(@无法保存!%@%@,错误,[错误localizedDescription]);
}
}
NSArray * orderItems = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
返回 OrderItem
对象的数组,您可以使用关系访问product和order
属性,例如:
OrderItem * oi in orderItems){
Product * product = oi.product;
Order * order = oi.order;
//显示product.productName,oi.qty,order.order-id,...
}
这假设您从 OrderItem 到产品之间具有产品
的关系
以及从 OrderItem 到订单的关系 order
。
请注意, OrderItem 的属性 order-id,product-id
是多余的,
作为相应的产品和订单之间的关系。
(换句话说,Core Data使用关系而不是外键。)
问题:假设 orderToProd
是从
OrdersItem 到产品的一对一关系,将执行以下操作以在新创建的对象之间建立关系
:
Product * entDesc = ...
// ...
OrderItems * orders = ...;
// ...
orders.orderToProd = entDesc;
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]); } }
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;
这篇关于从核心数据获取多个实体的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!