开发者

CakePHP HABTM relations listing

开发者 https://www.devze.com 2022-12-21 12:55 出处:网络
I\'m have quite a problem while trying to make a list (for the admin section) of HABTM relations. Here\'s the deal:

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.

开发者_StackOverflow中文版

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?


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'])
}
0

精彩评论

暂无评论...
验证码 换一张
取 消