views:

47

answers:

1

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?

+1  A: 

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'])
}
KienPham.com