NSPredicate +项目位置 == 位置
我遇到的情况是我有项目数据库。每个项目可以有多个位置。
现在有 2 个问题:
1 我应该创建每个项目链接到多个位置的关系,还是有办法将其记录到带有逗号的单个字符串中,例如 @"45,34,32"(数字是位置)。
如果我要建立这种关系,就会引发另一个问题。我正在记录 XML 中的项目,那么我应该如何形成和记录它,因为在这种情况下,场景可能是以下
项目 1 - 位置为 1,2,3 第 2 项 - 具有位置 1、2、5
以及如何将其插入数据库。
2 如何实际形成谓词,
它会像
@"brand_id LIKE %@ AND item_stock != 0 AND item_location.locations == %i"
我希望上面的内容不会令人困惑,非常感谢的回应。
I'm having the case where I have the database of items. Each item can have multiple locations.
Now 2 questions:
1 Should I create 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是需要关系时的典型示例(据我所知,在这种情况下为“多对多”,因为每个项目可以有多个位置,并且每个位置可以分配给多个项目)
“位置”
这样的结构现在可以存储多对多关系。
现在要查询这样的结构,您可以这样做(假设您搜索“Item”实体):
@"brand_id == %@ AND item_stock != 0 AND ANY Locations == %d"
当您的数据以 XML 形式传入时,您应该将其解析出来并分别添加每个位置,如果位置表中已存在此类位置,只需将其添加到 Item 的关系集中即可
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)
"Location"
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