I have a question with relationships in my CoreData model.
I have two Entities for now. 1. Flight, 2 Airport
I have attached the screenshots of the model to this post.
The question I have is how do I related the flight to the airport. In my SQL Databased schema I have foreign keys for each but in CoreData how can I set up this relationship correctly.
Essentially the Flight has an Origin and a Destination.
Any help would be appreciated.
AV
You can create two separate relationships between the two entities:
An “origin” relationship from Flight to Airport (to-one), with a to-many inverse relationship from Airport to Flight, “flightsStartingHere”, and
A “destination” relationship from Flight to Airport (also to-one), with a to-many inverse, “flightsTerminatingHere”.
Life gets more complicated if your flights have multiple stops. In that case I would model each Flight as having several Legs, and each Leg an origin Airport and a destination Airport. But the details will depend on the SQL database you are emulating.
Related
I would need some help with a problem we're facing in a company, trying to model every process and entity.
So far we have used an enhanced conceptual model with entities and attributes with relationships but there are some objects that don't exactly match a dimension or a fact table, and this is an entity that can be called "Shops with sales over X units". There is the entity "sales" and "shop" obviously, that would have it's representation in UML as independent entities and represent at the lower level, each sale and shop.
What we need to indicate in UML is an entity that stores the counter of shops with sales over X units, so this has some kind of behavior or conditions.
If we consider the entity, it would need date-from and date-to, and the value (counter), and creating a connection with the shop entity seems enough, but we miss the behavior that expresses "more than x sales". So the behavior could be for example: Go to the shop entity, take the 1st element and navigate to the sales entity, calculating the sales. If it's over X, then value+1, and so on.
I made a simple version of the problem. Blue boxes represent the entities already created, and the orange one is the counter that should count the shops with some constraints.
Is there any way of using some kind of UML diagram that can help us to solve this problem?
You could realize that with an association class:
ShopSales relates Shop and Sales so you can store the number of sales along with other things you might need in that conjunction. The ShopSalesStats could give you the shops by number of sales.
Another (of many) way(s) would be to just hold the count as public property of Shop and let ShopSalesStates handle the counts on all associated Shops.
I'm trying to build a single NSFetchRequest predicate for the following (simplified) model:
There is a simple to-many relationship between a Category and a number of Brands.
Each Brand then has a modelNumbersData property which is binary data, a serialisation of an array of NSString modelNumbers which are exposed in a transient property on the Brand objects.
There is no direct relationship between Brands and Models. The relationship is that a Model's modelNumber may be in a Brand's modelNumbers transient property.
I would like to build an NSPredicate query to fetch all of the Model objects under a particular Category.
Fetching the Models for a Brand is easy, I can do "modelNumber IN $FETCH_SOURCE.modelNumbers". How do I now extend this query to originate with the category? It seems I need a SUBQUERY?
Furthermore, I am doing an NSFetchRequest, so unless I'm mistaken I need to start with "SELF.modelNumber IN (...)", so that we select from all Models.
Thanks in advance!
Ok, I got there first. For reference, the following works:
SUBQUERY(%#.brands, $brand, $brand.modelNumbers CONTAINS $modelNumber).#count > 0
I think the problem I was mostly having was neglecting the .#count component. Still don't quite understand this, but it seems to be necessary with all SUBQUERY statements.
I have read many about how to configure many to many realtionships with EF Code First and fluent configuration. But I couldn't understand why I should create an extra entity for the junction table. Lets assume that I have the following entities:
Department
----------
Id
Name
Material
----------
Id
Name
Now I want to keep records of materials for each department. So I need
DepartmentMaterial
-------------------
DepartmentId
MaterialId
Quantity
What I have read so far I need 3 entities: Department, Material and DepartmentMaterial. So actually I am mapping each table to a corresponding entity which was not I intended to do when I started to learn about DDD. I assumed that EF is going to map that junction table automatically and also queries about material quantities can be done over Department.
Hence, is it possible in EF Code First to configure fluently such a relation without an extra entity?
The junction table can stay out of the conceptual model (class model) if it's only got two foreign keys (DepartmentId, MaterialId). You want to record some data about the association. That's OK, the quantity belongs there. A department has quantity x of material y. Saying that a department has a quantity is meaningless.
This is not against DDD principles. The association class is a first-class citizen in the class model. It could even have its own behavior (like limiting quantities for certain department/material combinations).
This means that your model will not contain a many-to-many association but Department 1-n DepartmentMaterial n-1 Material. And you'll have to query materials of departments through the junction table. This is a very common situation. I hardly ever see pure junction tables in real life. They're collector's items.
Example query:
var query = from d in db.Departments
from dm in d.DepartmentMaterials
select new { Department = d, Materials = dm.Select(x => x.Material)};
I have an entity User entity which is a parent entity to a Friend entity.
The User entity has a to-many relationship with an entity named Article.
The idea is there are users saved and also friends (from Facebook), the to-many relationship to Article is set on the User entity as this is being subclassed by Friend.
My question is how can I request all Articles by Friends and not by all users ?
I am having trouble setting an Predict to omit User entities and keep only Friend entities.
Thanks.
Is there a reason you are building a parent/child entity design here? Parent/Child entities come with a really heavy cost if you are persisting to a sqlite store. It is almost always a better idea to just have a single entity and have a flag stating if it is a friend.
Also, I know of no way, at the database level, to filter out the parent from the child in this design. It can be done in memory after the fetch by requesting the entity.name but that will not turn into a sql call properly.
What is an example of an inverse relationship in Apple's Core Data framework?
(Better late than never)
When you have 2 or more entities then you can have a relationship. Say for example , there are 2 entities: Book and Publisher. We have a very simple relationship between them as:
Every book has a publisher and a publisher may publish many book.
(In coredata , relationship is not an entity like in RDBMS. Infact relationship is a part of 1 entity. Relationship between A and B in coredata means , A store the reference of another entity. So, when the managed object is created from the entity A,then relationship will become a property of any object created from entity A.)
In the above example, book to publisher is one-to-one relationship and from publisher-to-book is one-to-many. That means book and publisher has two way relationship no matter it's 1-to-1 or 1-to-many , this bidirectional relation is set to inverse in coredata.This kind of relation is known as inverse relation. If you set the book as a inverse to publisher then automatically publisher becomes inverse of book.
It's not technically essential but highly recommended by apple.If one is changed another is affected. What this let us do is keep the object graph more controlled and consistent.Most relationships are bidirectional like this.
Source: Lynda.com
There's one simple explanation: http://brandontreb.com/core-data-quicktip-inverse-relationships/
Definition from Google:
"in·verse
ˈinvərs,inˈvərs
adjective
1.
opposite or contrary in position, direction, order, or effect.
"the well-observed inverse relationship between disability and social contact"
noun
noun: inverse; plural noun: inverses
1.
something that is the opposite or reverse of something else.
"his approach is the inverse of most research""
What you are looking for from Apple:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/TroubleshootingCoreData.html#//apple_ref/doc/uid/TP40001075-CH26-SW1
"Core Data uses inverse relationships to maintain referential integrity within the data model. If no inverse relationship exists and an object is deleted, you will be required to clean up that relationship manually."
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/HowManagedObjectsarerelated.html
"Inverse Relationships
Most object relationships are inherently bidirectional. If a Department has a to-many relationship to the Employees who work in a Department, there is an inverse relationship from an Employee to the Department that is to-one. The major exception is a fetched property, which represents a weak one-way relationship—there is no relationship from the destination to the source. See Weak Relationships (Fetched Properties).
It is highly recommended that you model relationships in both directions, and specify the inverse relationships appropriately. Core Data uses this information to ensure the consistency of the object graph if a change is made (see Manipulating Relationships and Object Graph Integrity)."
A great example is the second answer here and it should be upvoted:
Does every Core Data Relationship have to have an Inverse?
Try this (First google result for 'Core Data relationship tutorial iphone') :
http://www.raywenderlich.com/934/core-data-tutorial-getting-started