我怎样才能构建一个“has-many-through”?链接两个以上模型的关系?

发布于 2024-12-07 19:05:18 字数 1591 浏览 1 评论 0原文

我有 3 个型号,例如;

TABLE `users`
    `id` INT
    `username` VARCHAR(32)
    ...

TABLE `books`
    `id` INT
    `title` VARCHAR(100)
    `author` INT (foreign ket constraint)

TABLE `rights`
    `id` INT
    `name` VARCHAR(32)

现在我希望用户拥有特定的权利,例如。阅读或编辑一本书。所以权限表应该看起来像(很像 ORM 角色表):

|----|------|
| id | name |
|----|------|
| 1  | view |
| 2  | edit |
| .. | ...  |

我会有第四个表链接所有三个表;

TABLE user_book_rights
|---------|---------|----------|
| user_id | book_id | right_id |
|---------|---------|----------|
|    1    |    1    |    2     |
|    1    |    2    |    1     |
|    2    |    1    |    1     |
|   ...   |   ...   |   ...    |

因此,如果用户想要阅读一本书,我想检查 id 为 1 的登录用户是否具有 id 1 阅读 id 2 书籍的权限。

但是我到底如何使用 ORM 来实现这一点呢?当然,我可以编写自己的查询;

SELECT COUNT(*) as `has_right` FROM `user_book_rights` WHERE user_id=1 AND book_id=2 AND right_id=1

if($result['has_right']) {
    echo 'Yeah, read the book!';
} else {
    echo 'Sorry mate, this book is not for dummies...';
}

但我宁愿做这样的事情:

$has_right = $user->has('book_rights', ORM::factory('user_book_right', array('book_id' => '2', 'right_id' => 1));

或者甚至更好:

$book = ORM::factory('book', 1);
$right = ORM::factory('right', array('name' => 'view'));
$has_right = $user->has('book_rights', ORM::factory('user_book_right', array($book, $right)));

我找不到我的问题的答案。想要将三个模型链接为 Many_through 关系是不是很奇怪?或者 ORM 没有能力,我应该编写自己的查询吗?

谢谢。为了您的见解!

I have 3 models eg;

TABLE `users`
    `id` INT
    `username` VARCHAR(32)
    ...

TABLE `books`
    `id` INT
    `title` VARCHAR(100)
    `author` INT (foreign ket constraint)

TABLE `rights`
    `id` INT
    `name` VARCHAR(32)

Now I want a user to have particular rights to eg. read or edit a book. So the rights table should look like (much like ORM roles table):

|----|------|
| id | name |
|----|------|
| 1  | view |
| 2  | edit |
| .. | ...  |

And I would have a fourth table linking all three;

TABLE user_book_rights
|---------|---------|----------|
| user_id | book_id | right_id |
|---------|---------|----------|
|    1    |    1    |    2     |
|    1    |    2    |    1     |
|    2    |    1    |    1     |
|   ...   |   ...   |   ...    |

So if a user wants to, say, read a book, I want to check if the logged in user with id 1, has the right with id 1 for book with id 2.

But how the heck can I achieve this with ORM? Of course I can just write my own query;

SELECT COUNT(*) as `has_right` FROM `user_book_rights` WHERE user_id=1 AND book_id=2 AND right_id=1

if($result['has_right']) {
    echo 'Yeah, read the book!';
} else {
    echo 'Sorry mate, this book is not for dummies...';
}

But I'd rather do something like:

$has_right = $user->has('book_rights', ORM::factory('user_book_right', array('book_id' => '2', 'right_id' => 1));

Or even better:

$book = ORM::factory('book', 1);
$right = ORM::factory('right', array('name' => 'view'));
$has_right = $user->has('book_rights', ORM::factory('user_book_right', array($book, $right)));

I could not find an answer to my question. Is it weird to want to link three models as a many_through realtionship? Or is ORM just not capable and should I write my own query?

Thanks ia. for your insights!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

养猫人 2024-12-14 19:05:18

也许我的回答对你帮助不大。但我建议您以位表示形式对右侧进行编码。

READ = 00000001 = 1
EDIT = 00000010 = 2
DELETE = 0000100 = 4

如果用户具有读取、编辑和删除的写入权限
操作

READ | EDIT | DELETE = 0000111 = 7

如果您想测试用户是否拥有特定权利,您只需执行以下 :
if ($user_write & READ) { // he can read}

也许如果您使用这种设计,并消除带有这些常量的权限表,它可能会对您有所帮助。

Maybe my answer does not help you a lot. But i suggest that you encode the right in bit representation.

READ = 00000001 = 1
EDIT = 00000010 = 2
DELETE = 0000100 = 4

than, if a user has the write to read, edit and delete
you just do

READ | EDIT | DELETE = 0000111 = 7

If you want to test if a user has a particular right you just do:
if ($user_write & READ) { // he can read}

Maybe if you use this design, and eliminate the rights table with those constants, it may help you.

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