I have a data model with two entitys with relationship one to many. Lets call them City<-->>Person.
In my tableview Im currently displaying each City, seperated by sections (currently one cell in each section).
Is there any way for me to add each Person connected to the specific city in the corresponding section? (adding as many cells as number of persons connected to the city)
Im using two different fetchedResultsControllers to access the Entities but cant quite figure out how to do this.
Ultimately Id like each sections first cell to be a city, followed by all the persons in that city.
Any help or pointer in the right direction would be highly appreciated.
As Michal mentioned in comments. A cleaner design would be for your City entities to be displayed in the section headers (pretty easy) and have your person objects in the sections as rows.
To do that you need to add a unique identifier from the city as your section path of your NSFetchedResultsController. Since that attribute takes a key path you can specify something like city.name and it will resolve through the relationship from person to city and get that value.
Your only other option would be to manually fetch the city objects and build your UITableViewDatasource to talk to your own construct of data. Doable but far, far more difficult than what Michal suggested.
Related
The problem is as follows. Lets say I have a class Person which has the data property hasAge which points to an integer value. I would now like to anonymize a Person in a way that there would be like a class PersonANON so I could have for an individual Person1 a PersonANON1. The age of PersonANON1 should e.g. be rounded to the next multiple of five or in some other way transformed.
I thought about how to solve this. Creating PersonANON as an equivalent class to Person yields the same age which cannot be modified. I hoped in some way I could transform the data later on.
The only solution I came up with would be to have the two classes separate from each other and insert the data manually by externally querying the data, transforming it and creating a new individual with the fitting data in another query.
This does not seem very nice so I was hoping someone of you has a better idea. Maybe there is a way to leverage some ontology design feature that I am not aware of helping me.
Thanks in advance!
I'm creating an application in which I have several entities and now I need to filter the content of third combobox dynamically. I explain myself better. I have 3 combobox (building, floor and department), I would like first to show me all the buildings included, but the second should show only selected before the plans for the building, the last I should be select only the departments of the building and the plan you choose. How can I do this? To simplify attaching some photos.
You simply drill down with predicates, if you use single fetch requests to Core Data.
However, your relationships are not set up correctly. For example, there is an edificio attribute in Particelle. If it refers to an building, it should be a relationship to a Edifici object, not some kind of foreign key. There are no foreign keys in Core Data, just relationships.
If you do this, everything becomes much easier by using a NSFetchedResultsController. You can now simply traverse the object graph without any specific fetching.
The scheme could be something like this (maybe need to change the order):
Anno <--->> Particella <---->> Edificio <---->> AreaRischio
Now you can simply tell the fetched results controller to start fetching all Anno entities. Then you drill down with simple dot notation:
NSSet *listForNextTable = selectedAnnoObject.particelle;
and further with
NSSet *listForNextTable = selectedParticellaObject.edifici;
etc. You see, it gets really simple.
I have an example application I'm working on to help me learn about Core Data. In this application I created a model consisting of the entities "Friend" and "City". The application list my friends and which city they are from in an NSTableView. In this table view I would like to have the City column be an NSComboBoxCell with a list of the cities. I got this far... now for the problem:
When I select the city from the combo box, the application takes the value of the selected city name and applies it to the name of the city the friend is currently from. Instead, I would like the application to actually change the city the user is from and not the name of the city... That's a bit confusing of a question, so here's an example: starting with a list of friends like
Andy Asheville
Francois Montreal
Jeff Asheville
If I use the NSComboBoxCell to change the city for Andy from Asheville to Montreal, the application actually changes the name of the City Asheville to Montreal, so the result looks like:
Andy Montreal
Francois Montreal
Jeff Montreal
There are still two distinct cities in the application, but they now both have the name Montreal.
This all makes sense to me given the way I set up my bindings. I bound the value of the city table column by setting Model Key Path to "city.name" and the Controller Key to arrangedObjects, which contains the list of friends. So of course, when the value of a cell changes it modifies city.name. So then my question becomes what's the proper way to do this so that the city changes instead of the city name?
The purpose of NSComboBox is to "allow you to either enter text directly (as you would with an NSTextField) or (...) select from a displayed list of items". (taken from Apple Cocoa Ref).
So The combo box is a glorified NSTextField. The behavior you expect is more in line with the NSPopupButton.
As you noted, the NSComboBox bindings change the name value of the current City, but do not alter the object relationship, hence they change the name of the current city represented by the relationship (which then gets "propagated" to other Friends pointing to the same City entity).
If you look at the available bindings for NSPopupButton you'll see the difference. You probably want to use the NSPopupButton(Cell) to assign Cities to Friends, with some sort of "on the side", NSTableView-based editor to manage your city names — which are really two distinct tasks.
I really hope someone can help on this because I'm learning cocoa and have hit a road block.
I am trying to model a simple poker tournament. For now, my entities are simply a Tournament (with a number) and a Player (with a Name). A Tournament has an array of Players.
I can bind two independent table views to display the tournaments and the players just fine. But I want the players table view to just show the players that belong to the selected tournament from the first table view.
Each has it's own array controller. I have tried a variety of different bindings for the second (players) table but to no avail. Has anyone accomplished this? If so maybe you could spell it out for me, as I there are few examples online.
Update
I can now ALMOST get where I need to, mostly through rial and error and hours of googling. I have bound the player AC's content to the tournament AC, with controller key 'selected objects' and Model Key Path 'players', which is the name of the array in my Tournament entity.
I have the bound the column in the players table view to this second Player AC, controller key arranged objects. But what to put in the Model Key Path? I know it is working because if I stick #count in there I get the correct number of players for the selected tournament. But 'name' and 'player.name' are no good. Is there any kind of 'item.name' or 'players.item.name' I can try?
Sooo close, thanks for the help so far:
I think this tutorial will help you. They also create a master/detail view.
In short: Bind the contentArray of your player's array controller to the tournament's array controller, set ControllerKey to selection and the remaining properties accordingly to your model.
I found the answer here:
Implementing parent->child drill down in Cocoa with Core Data bindings that span multiple entities.
The child controller needs to know about the managedObjectContext through its own binding.
The child controller must not be in Entity Mode, but rather operate as a NSMutableDictionary class.
And, finally, the child controller does not prepare its data. It retrieves it from the parent, through the Content Set binding. Use the controller key selection, and the model key path that connects to the children.
I'm surprised this is not a more commonly used practice, and hope the next person reading this doesn't spend so long finding the answer!
I'm having a hard time designing a relationship with a few models in my project.
The models are: band, musician, instrument
Bands have multiple musicians
Musicians have multiple bands and multiple instruments
That’s all pretty straightforward, but I also need to keep track of what instruments a musician has for a particular band. So in a sense, I guess, bands have multiple instruments via the musicians.
In the tables, I was going to add instrument_id to the bands_musicians linking table, but I need a musician to be able to have multiple instruments for a band, so I was thinking it would need to go in the musicians_instruments table.
What's the best way to set up the relationships with these models?
Thanks for your time!
Musicians would have a one-to-many relationship with both bands and instruments. So create your musicians table and add all of the information relavent to the musicians themselves into that table.
Create an instruments table to hold information about instruments, and do the same for the bands. That will take care of all of your individual items.
Then create something like 'band_assignments' table that just has the id of a band and the id of a musician and links the two together. Create an 'instrument_assignment' table to do the same thing.
Now when you query a musician you can left join all of these tables together to get the data that you need or selectively join on just instruments, just bands, or sort by 'join date' and limit to get the last band they joined or the last instrument they learned.
Basically 5 tables should cover it all.
musicians (musician_id, first_name, last_name)
bands (band_id, name)
instruments (instrument_id, name)
band_instument_assignments (musician_id, band_id, instrument_id, date_played)
As you can see in the edited version above you will have multiple rows in the 'band_instrument_assignments' table--one for each instrument that each user played in each band. You will need to use some GROUP BY and LIMIT clauses to get the data you want, but it should work for you.
See:
How to handle a Many-to-Many relationship with PHP and MySQL
That should give you an idea on how to go about designing your database structure.
someoneinomaha
Maybe you need 4th model, which will cover and union all of her children entities, e.g. called like 'Mus Model'(or whatever you want) and have some methods like:
get_bands()
get_instruments()
get_musicians()
get_instruments_by_musician()
get_musicians_by_band()
get_instruments_by_band()
get band_by_musician()
and so on...It'll provide you needed data and will not brake entities relationships, imho.
I might be a little late to the party here and I am no database expert but I have found that drawing out your DB schema helps immensely. Just make boxes and fill in your table names and columns then draw arrows to define your relationships and it should be a lot clearer as to how you should structure things and whether you need to add a table to join two other tables.
If all else fails, just copy a schema from databaseanswers.org. I'm sure there is one there that would probably help you.