【问题标题】:Post visibility for specific users特定用户的发布可见性
【发布时间】:2015-07-16 12:25:52
【问题描述】:

我即将开始编写一些类似这样的网站:

用户登录,如果他是普通用户(不是管理员),他会看到一些与他的帐户类型相关的帖子(帐户由管理员管理和提供)

管理员所做的是创建这些帖子,并在创建帖子时说谁将有权查看该帖子(例如检查将看到该帖子的用户,或者多个用户或所有用户都可以看到它)

我的问题是,我应该在我的数据库中创建什么样的表以及使用哪些列。 我的第一个计划是拥有 ofc: 1) 用户表 2) 职位表 3) 权限表 权限表将有字段 ([postID],然后是 [user1],[user2]....[userN]) 字段,表中的示例行看起来像 21|true|true|false|true|....|true| 表示这些用户将能够看到帖子。并且这些字段 [userN] 可以在创建新用户时动态创建 我正在征求对这种数据库的意见,当然还有你做这种数据库的想法。

【问题讨论】:

    标签: php mysql database database-design


    【解决方案1】:
    create table users
    (   id int not null auto_increment primary key,
        fullName varchar(100) not null
    );
    
    create table posts
    (   id int not null auto_increment primary key,
        postName varchar(200) not null
    );
    
    create table post_user_junction
    (   id int not null auto_increment primary key,
        userId int not null,
        postId int not null,
        UNIQUE (userId,postId),
        -- foreign key (FK) referential integrity:
        FOREIGN KEY (userId) REFERENCES users(id),
        FOREIGN KEY (postId) REFERENCES posts(id)
    );
    
    insert post_user_junction (userId,postId) values (1,1);
    -- ooops, Error 1452, FK violation, user and post do not exist yet
    
    insert users(fullName) values ('a');
    insert posts(postName) values ('a');
    
    -- works:
    insert post_user_junction (userId,postId) values (1,1);
    
    -- do it again, does not work, already there:
    insert post_user_junction (userId,postId) values (1,1);
    

    你去吧,你应该走出大门,至少开始。

    【讨论】:

    • 感谢您的回复,但如果我希望某个帖子对 2 位或更多用户可见,但不是所有人都可以看到,该怎么办?我应该在表中为每个“后用户”关系添加行吗?我只是觉得这有点多余,但如果这是一条路……?
    • 现在在post_user_junction 中,您可以根据需要添加带有诸如READONLY、UPDATE 等值的rights 列。或者不添加,如果存在连接行,则用户拥有所有权限。随便。
    猜你喜欢
    • 2015-04-21
    • 2021-12-07
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2012-03-23
    • 1970-01-01
    相关资源
    最近更新 更多