Xcode 4 - Adding and associating an object to an entity using relationships - core-data

I've been learning how to use Core Data in xcode for a couple of weeks now but have been stumped on how to get the relationships to work.
In my code I have 2 entities. The "House" entity and the "City" entity. A house can only belong in 1 city, but a city can have many houses associated with it. So I set up a one-to-many relationship between the house and city entities.
The following code snippet is in the city view controller, which is just a table view that displays all available cities to choose from. The list is pre-populated in the city entity. Now when I select a row I try to execute the following code to associate a house with the selected city but the result is always nil.
The referringObject is the house object that I am creating
cities is the relationship from the house to the city entity
Any help is appreciated
NSManagedObject *selectedCity =
[self.fetchedResultsController objectAtIndexPath:indexPath];
[self.referringObject setValue:[selectedCity valueForKey:#"cityname"]
forKeyPath:#"cities.cityname"];

You want to associate the city object with the house object. You don't need to change the cityname value inside of it. That way you only have one entry in the table for each city. So what you want to do is:
NSManagedObject *selectedCity = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.referringObject setValue:selectedCity forKeyPath:#"city"];
That'll set up your both relationships for you. You can then access the city's name from a house object with houseObj.city.cityname or get the list of houses in a city with city.houses (assuming you named the relationship houses).

Related

how to get a userIds or users associated with particular assets category

If I create a new category it will store in AssestCategory table.At the same time entry should be in AssestEntry table also.For mapping between these two tables we have AssestEntries_AssestCategories table.How to achieve above thing by programmatically because for mapping table I didn't get any API.by these tables shall I get userIds associated with particular category. Or let me know is there any other way.
A new entry stored in AssetCategory when a category is created for a topic in site.
For example, Create a category named "Test Category" in site. You will see an entry in AssetCategory table. But there will not be any entry get stored to AssetEntries_AssetCategories table.
When the category is associated with some data item, then its association is stored in AssetEntries_AssetCategories table.
For example, When you create a bookmark with "Test Category" selected, then a new entry stores in AssetEntries_AssetCategories table.
entryId in AssetEntries_AssetCategories represents the corresponding entry for AssetEntry table.
In liferay Asset framework, liferay-ui:asset-categories-selector tag can be used to provide Asset category selection.
Please refer below links for details of liferay asset framework and to implement the same for a custom resource.
https://www.liferay.com/en_GB/documentation/liferay-portal/6.2/development/-/ai/asset-framework-liferay-portal-6-2-dev-guide-06-en
https://dev.liferay.com/develop/learning-paths/-/knowledge_base/6-2/enabling-assets-at-the-service-layer

Is it possible to query N:N relationships in a Dialog Query CRM Data step?

I have a custom entity called Department with a N:N relationship with the User entity. I have a lookup field on the Case entity for Department. I added some javascript to the onChange event of the Department field that will query the Department entity and get the users associated with it. If there is only 1 user it sets the Assigned To field on the case to that user. If there are more than one it kicks off a dialog that lets the user select which of the users associated with the Department that the case should be assigned to.
In the Dialog I'm using a Query CRM Data step to get the Users associated with a department but I can't figure out how. I thought with the N:N there would be an entry in the Related section when building the query. There are entries for Departments but they are for Created By, Modified By, and Owning User. Is it possible? Is there a better way to do what I'm trying to do?
You need to use a SOAP call to retrieve all the Users associated with Department. For every N:N relationship there will be an Intersect entity and you need to identify that intersect entity name from N:N relationship in System Customization as in below fig. Here I'm retrieving all the Teams associated with User. In the same way you can retrieve the Users by passing Department Id.
Note: Replace " <q1:Operator>EqualUserId</q1:Operator>" + with below code
" <q1:Operator>Equal</q1:Operator>" +
"<q1:Values>" +
"<q1:Value xsi:type=\"xsd:string\">" + _departmentId + "</q1:Value>" +
"</q1:Values>" +

Create custom view with multiple entities in CRM 2011?

I'm new in CRM 2011. So not familiar with all it functionality.
Have a question about displaying data from multiple related entities.
First Entity is a Users (contain information about user)
Second Entity is a Class (contain information about class user enrolled)
Third Entity is a Class Attendance (contain information if user attended class on specific date)
The idea is to show view with users who is enrolled in class.
To show start end dates and if user attended class or not.
Administrator should select user and change status to attended or not.
How it can be done in CRM 2011? Is it required custom development or just to View Customization using user interface in CRM?
Any suggestion or examples highly appreciated.
In Mscrm you can create a view for a single primary record type, on this view you can show fields from the primary record. You can also include secondary records that are linked via a lookup to the primary record, you can show columns of these secondary linked records. The limitation here is that you cannot link tertiary records from these secondary linked entities. The slight confusion here is that you can show the name of the tertiary entity as its a field on the secondary entity.
So to put this into the context for a real example, take the following entity model.
Contact has a 1 to Many relationship with incident (a contact can have many incidents, each incident has a lookup to contact)
Incident has a 1 to Many relationship with task (an incident can have many tasks, each task has a lookup to an incident)
So a view of tasks can;
Show all the fields from the task (task in the primary)
Show all the fields from the incident (incident is the secondary)
Show the name of a the contact, but not any other field (contact is a tertiary, but its name is shown as a field from the incident)
This is one of those things that becomes a lot clearer when you actually try it out for yourself. Its hard to say what you will be able to achieve because its not clear what your entity model is.
In any case here is how you create your own custom view.
CRM > Settings > Solutions > Your Solution > Your Entity > Views > New. There are various buttons would should hopefully be self explanatory.
Click Add Columns and it will present you with fields of the primary entity, use the Record Type drop down option to select fields from linked secondary entities.
Reading between the lines you have:
Class entity with many:many relationship to student (user), so a user can be enrolled on more than one class and a class has more than one user.
There is then a second entity for attendance which by the sound of it has a many:1 relationship to both student and class, and has a status to show attendance for a particular instance of the class on one date.
So far sounds like a good model.
Using the ideas in James' answer you ought to be doing a view of Attendances, including the name of the class and user. You might also have extra columns from the user or class to show things like when the course starts and ends, what the student's email address is etc.
Sort this view by class, then student, then attendance date and you have a pretty good view. But this won't display in any kind of hierarchy or show summaries. You might want to look at building a custom report for this instead so you can report on attendances, grouped by student and grouped by class. If you get clever you could also add filters for dates so you can look at attendances last month only, for example. You can probably do a lot of this with the built-in report wizard, for more complex or pretty versions go to SSRS

Core Data - Delete rule

I'm following Apple's documentation about Relationships. In their example, a Department has a to-many relationship to the Employees that work in a Department, and there is an inverse relationship from an Employee to the Department.
What should be the delete rule from Employee to Department? When I delete an employee, I don't want to change anything in the department, so the rule would be "No Action", which gives me a warning. I suppose I'm missing something here.
Nullify, which means that if a department is deleted the relationship on the Employee site will be set to nil.

Core Data Relationship shown in tableView

I am developing an application with a tableview showing the content of a core data table.
The datamodel is something like this:
Entity(name, code)->>Translation(text, code)
I retrieve all the entities using the usual NSFetchedResultsController, but then once is time to populate each row (through tableview cellForRowAtIndexPath:) I have to dive into each entity to retrieve 2 translations based on the code inputed by the user. I am using a NSFetchRequest to do that but I was wondering if it is the right thing to do (one fetch request each time I populate a row).
Instinctively I would retrieve all the data I need in the NSFetchedResultsController, instead of searching for each translation each time I populate a cell, but I cannot figure out how.
Do anyone has some advice, or maybe some interesting links?
If each cell must display the Translation objects related to each Entity object, then you only need to walk the relationship from the fetched Entity object to the appropriate Translation objects.
Once you've fetched the Entity objects and then structured the table to display them, then to access the values in the translations for each cell row like so:
NSSet *translations=[anEntityObject valueForKey:#"translations"];
... which returns a set of Translation object for the Entity object represented by the tableview row.
As a very general rule, you only do one fetch per tableview. A table view should be configured to display data related to one particular entity.

Resources