CakePHP HABTM 关系列表
我在尝试制作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要使用 foreach 并循环遍历您的结果来重建一个像这样的新数组。
I think you would need to use the
foreach
and loop through your result to reconstruct a new array like that.