【问题标题】:N to N relation in items in same table同一张表中项目的 N 到 N 关系
【发布时间】:2018-04-28 17:08:40
【问题描述】:

我正在使用 Grocery CRUD(创建全功能 CRUD 的 PHP 库),并且我有非常基本的用户表。

标识 |名字 |姓氏

其中一些用户是亲戚。所以我想要一个单独的关系表,看起来像

标识 | first_users_id | second_user_id |关系类型

如何将此关系添加到 Grocery CRUD,以便在编辑用户个人资料时选择其他为每个人提供关系类型的亲戚用户?

【问题讨论】:

    标签: php relational-database grocery-crud


    【解决方案1】:

    没有太多事情要做,你应该能够像这样完成它:https://www.grocerycrud.com/examples/set_a_relation_n_n

    你也可以参考这里的文档来使用函数https://www.grocerycrud.com/documentation/options_functions/set_relation_n_n

    $crud->set_table('user_table');
    $crud->set_relation_n_n('relatives', 'user_user_table', 'user_table', 'id', 'id', 'id');
    

    【讨论】:

    • 不幸的是它不起作用 - '字段列表'中有错误未知列'user_table.relation_type'
    • user_table 需要与您的表的实际名称匹配,user_table 与您提到的第一个表对齐,user_user_table 与您提到的第二个表匹配
    • 我明白了。但正如您在这种情况下看到的错误,relation_type 字段需要在 user_table 中。我在 user_user_table 中有这个字段。
    • 好的,现在我看到下拉菜单,我只看到用户的 ID。如果我能够看到他们的名字并指定关系类型,那就太好了。另外我应该如何提供有关将 id 保存到 first_users_id 和 second_user_id 的信息?
    • 如果我将代码中的最后一个 id 参数更改为 {last_name} {first_name} - 我看到了名字。但仍然对保存这些数据以及提供有关关系的信息有疑问。
    猜你喜欢
    • 2020-09-16
    • 2019-12-25
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多