NSPredicate +项目位置 == 位置

发布于 2024-12-08 21:19:12 字数 450 浏览 0 评论 0原文

我遇到的情况是我有项目数据库。每个项目可以有多个位置。

现在有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淤浪 2024-12-15 21:19:12

这是需要关系时的典型示例(据我所知,在这种情况下为“多对多”,因为每个项目可以有多个位置,并且每个位置可以分配给多个项目)

  1. 创建 2 个实体“项目”和“ Location”
  2. 为“Item”创建一个关系“locations”,这是一个对多关系
    “位置”
  3. 对“位置”实体执行相同的操作(反之亦然)

这样的结构现在可以存储多对多关系。

现在要查询这样的结构,您可以这样做(假设您搜索“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)

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文