从不同实体中选择列 [英] Selecting columns from different entities

查看:82
本文介绍了从不同实体中选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否应该画平行线,但是不幸的是,这是表达问题的唯一方法。
在SQL中,假设我们有两个表:

I don't know whether I should be drawing parallels, but unfortunately, that's the only way I can express my issue. In SQL, suppose we have two tables:


  1. 带有雇员ID,雇员姓名,部门ID列的雇员

  2. 带有列ID的部门,部门名称

部门ID。

现在假设我要获取以下列:
Employee ID,Employee Name,部门名称

Now suppose I want to fetch the following columns: Employee ID, Employee Name, Department Name

使用SQL,例如:

SELECT A.EMPLOYEE_ID, A.EMPLOYEE_NAME, B.DEPT_NAME
FROM EMPLOYEE A, DEPARTMENT B
WHERE A.DEPT_ID = B.DEPT_ID

如何在Swift中使用Core Data做到这一点?我想我只能看到对
NSFetchRequest(entityName:entityName)
的引用而感到困惑,其中,entityName指向单个实体(表关系DB术语)

How would one do this using Core Data in Swift? I guess I'm getting confused by only seeing references to NSFetchRequest(entityName: entityName) where the entityName refers to a single entity (table in relational DB lingo)

我非常感谢任何可以帮助我消除疑虑的指针或示例。

I'd really appreciate any pointers or examples that can help me clear my doubts.

谢谢

推荐答案

当然可以创建与您的SQL查询等效的提取请求。如果不是一个单一的提取请求就不可能实现更复杂的查询。但是,我建议不要在CoreData和SQL之间画出相似之处,至少直到您掌握它的工作原理为止。

It is certainly possible to create a fetch request that is equivalent to your SQL query. More complex queries can be difficult if not impossible to achieve with a single fetch request. But I recommend trying NOT to draw parallels between CoreData and SQL, at least until you have got to grips with how it works.

在CoreData视图中举例说明在世界范围内,雇员将是与另一个实体部门有关系的实体。基于 Employee 实体的提取请求将返回 Employee 对象的数组,并且(假设您创建NSManagedObject的子类对于每个实体),您都可以使用简单的点符号来访问属性:

To take your example, in the CoreData view of the world, Employee would be an entity with a relationship to another entity, Department. A fetch request based on the Employee entity will return an array of Employee objects, and (assuming you create subclasses of NSManagedObject for each entity) you can access the attributes with simple dot notation:

let employeeName = myEmployeeObject.employeeName

但是您可以使用相同的符号同样轻松地遍历关系:

But you can use the same notation to traverse relationships equally easily:

let departmentName = myEmployeeObject.department.departmentName

您不无需担心加入等;

You don't need to worry about joins, etc; CoreData handles that for you.

现在,假设您尝试以 SQL方式进行操作。您可以基于Employee实体构造一个获取请求,但是指定同样要遍历关系的要获取的属性:

Now, suppose you try to do it "the SQL way". You can construct a fetch request based on the Employee entity, but specify "properties to fetch" which likewise traverse the relationship:

let fetch = NSFetchRequest(entity:"Employee")
fetch.propertiesToFetch = ["employeeID", "employeeName", "department.departmentName"]

要执行此操作,您需要将获取请求的 resultType指定为DictionaryResultType:

For this to work, you would need to specify the "resultType" for the fetch request to be DictionaryResultType:

fetch.resultType = .DictionaryResultType

此查询的结果为包含相关键和值的字典数组。但是与基础对象的链接丢失了。如果随后要访问相关部门(甚至是 Employee )的任何详细信息,则必须运行重新获取对象本身的新操作。

The results from this query would be an array of dictionaries containing the relevant keys and values. But the link with the underlying objects is lost. If you subsequently want to access any details from the relevant Department (or even the Employee), you would have to run a new fetch to get the object itself.

这篇关于从不同实体中选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆