开发者

NSPredicate + itemLocations == location

开发者 https://www.devze.com 2023-04-10 18:27 出处:网络
I\'m having the case where I have the database of items. Each item can have multiple locations. Now 2 questions:

I'm having the case where I have the database of items. Each item can have multiple locations.

Now 2 questions:

1 Should I c开发者_Go百科reate the relationship where each item is linked to several location or there is a way to record it into a single string with comas for example @"45,34,32"(numbers are locations).

If I'm creating the relationship, it raises another question. I'm recording items from an XML so how should I form and record that , because in that case the scenario could be the following

item 1 - has location 1,2,3 item 2 - has location 1,2,5

and how should insert it into the database.

2 How to actually form the the predicate

will it be smth like

@"brand_id LIKE %@ AND item_stock != 0 AND item_location.locations == %i"

I hope the above is not confusing will really appreciate the respond.


This is a typical example when a relationship is required (in this case "many-to-many" as I understand, since each item can have multiple locations and each location can be assigned to many items)

  1. Create 2 entities "Item" and "Location"
  2. For "Item" create a relationship "locations" which is a to-many relationship to "Location"
  3. Do the same for "Location" entity (but vice versa)

Such a structure can now store many-to-many relationships.

Now to query such a structure, you could do smth like this (given you search on "Item" entity):

@"brand_id == %@ AND item_stock != 0 AND ANY locations == %d"

When your data comes in as XML, you should parse it out and add each location separately, and if such location already exists in the locations table, simply add it into the Item's relationship set

0

精彩评论

暂无评论...
验证码 换一张
取 消