CakePHP HABTM 关系列表

发布于 2024-08-22 20:03:03 字数 852 浏览 10 评论 0原文

我在尝试制作 HABTM 关系列表(针对管理部分)时遇到了很大的问题。事情是这样的:

权限:id,name; 用户:id、用户名; Permissions_users:permission_id,user_id

Permission HasAndBelongsToMany User

我想制作如下列表:

User.id | Permission HasAndBelongsToMany User用户.用户名 |权限.id |权限.name
1 |杰克| 1 |帖子
1 |杰克| 2 |评论
2 |马克| 1 |帖子
3 |凯特| 3 |标签

之类的东西: $this->Permission->User->find('all'); (或相反)并没有真正起作用,因为它将获取 Jack 的许多权限,反之亦然,它将获取许多用户的 posts 权限,从而使其无法在视图中列出。

我想要的是得到一个像这样的数组:

[0] = >数组(
[用户] => array([id] => 1 [用户名] => Jack)
[权限] => array([id] => 1 [name] => posts)
)
[1] =>数组(
[用户] => array([id] => 1 [用户名] => Jack)
[权限] => array([id] => 2 [name] => comments)
) ... 有

什么想法吗?

I'm have quite a problem while trying to make a list (for the admin section) of HABTM relations. Here's the deal:

permissions: id, name;
users: id, username;
permissions_users: permission_id, user_id

Permission HasAndBelongsToMany User

I want to make a listing like such:

User.id | User.username | Permission.id | Permission.name
1 | Jack | 1 | posts
1 | Jack | 2 | comments
2 | Mark | 1 | posts
3 | Kate | 3 | tags

Stuff like: $this->Permission->User->find('all'); (or the other way around) doesn't really work, because it will fetch many permissions for Jack, also the other way around it will fetch many users for the posts permission, thus making it impossible to list in the view.

What I want is to get a array like:

[0] = > array(
[User] => array([id] => 1 [username] => Jack)
[Permission] => array([id] => 1 [name] => posts)
)
[1] = > array(
[User] => array([id] => 1 [username] => Jack)
[Permission] => array([id] => 2 [name] => comments)
)
...

Any ideas?

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

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

发布评论

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

评论(1

街角迷惘 2024-08-29 20:03:03

我认为您需要使用 foreach 并循环遍历您的结果来重建一个像这样的新数组。

$user = array('id' => '1', 'name' => 'Jack');
$data = array();
foreach($permission as $per) {
   $data[] = array($user, $per['Permission'])
}

I think you would need to use the foreach and loop through your result to reconstruct a new array like that.

$user = array('id' => '1', 'name' => 'Jack');
$data = array();
foreach($permission as $per) {
   $data[] = array($user, $per['Permission'])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文