easy to solve at all. :(
If I just use bake to create scaffolded views and controller actions,
the edit form it generates correctly uses select inputs for
Permissions and Users, but they are both empty. I'm assuming that the
scaffolded code isn't just isn't smart enough to handle HABTM
relationships, so I've been attempting to write my own code to achieve
this. After spending many hours trying to figure out how to do this
the "cake" way, I gave up and used custom queries to make the form
appear correctly. Here is my code:
*** edit.ctp ***
<div class="groups form">
<?php echo $form->create('Group');?>
<fieldset>
<legend><?php __('Edit Group');?></legend>
<?php
echo $form->input('id');
echo $form->input('name');
echo "Permissions<br />\n";
foreach ($permissions as $id => $permissionData)
{
$checked = false;
if (array_key_exists($id, $this->data['Permission']))
$checked = 'checked';
echo $form->input('Permission.id.' . $id, array('type' =>
'checkbox', 'label' => $permissionData['name'], 'checked' =>
$checked));
}
echo "Users<br />\n";
foreach ($users as $id => $userData)
{
$checked = false;
if (array_key_exists($id, $this->data['User']))
$checked = 'checked';
echo $form->input('User.id.' . $id, array('type' => 'checkbox',
'label' => $userData['username'], 'checked' => $checked));
}
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
*** GroupsController.php ***
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Group', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
if ($this->Group->save($this->data)) {
$this->Session->setFlash(__('The Group has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Group could not be saved. Please,
try again.', true));
}
}
if (empty($this->data)) {
$result = $this->Group->find('first', array('fields' =>
array('Group.id', 'Group.name'), 'recursive' => -1, 'conditions' =>
array('Group.id' => $id)));
$this->data['Group'] = $result['Group'];
$unformattedPermissions = $this->Group->query("SELECT
`Permission`.`id`, `Permission`.`name` FROM `permissions` AS
`Permission`, `groups_permissions` AS `GroupsPermission` WHERE
`GroupsPermission`.`group_id` = " . $id . " AND
`GroupsPermission`.`permission_id` = `Permission`.`id`");
$formattedPermissions = array();
foreach ($unformattedPermissions as $unformattedPermission)
$formattedPermissions[$unformattedPermission['Permission']['id']]
= $unformattedPermission['Permission']['name'];
$this->data['Permission'] = $formattedPermissions;
$unformattedUsers = $this->Group->query("SELECT `User`.`id`,
`User`.`username` FROM `users` AS `User`, `groups_users` AS
`GroupsUser` WHERE `GroupsUser`.`group_id` = " . $id . " AND
`GroupsUser`.`user_id` = `User`.`id`");
$formattedUsers = array();
foreach ($unformattedUsers as $unformattedUser)
$formattedUsers[$unformattedUser['User']['id']] =
$unformattedUser['User']['username'];
$this->data['User'] = $formattedUsers;
$this->set('permissions', Set::combine($this->Group->Permission-
>find('all', array('fields' => array('id', 'name'), 'recursive' =>
-1)), '{n}.Permission.id', '{n}.Permission'));
$this->set('users', Set::combine($this->Group->User->find('all',
array('fields' => array('id', 'username'), 'recursive' => -1)),
'{n}.User.id', '{n}.User'));
}
}
------
This causes the form to appear exactly as I want: a checkbox for every
possible permission and every possible user, and the boxes default to
checked according to the current associations. However, if I make any
sort of changes and submit the form, it does not update the records in
the database to match what's on the form. All it does is change the
groups_permissions join table, setting permission_id = 0 for every
record with group_id = (the group being edited). There is no
permission with ID = 0. It should be adding and removing records from
this join table to match which boxes in the form were checked and
unchecked. It does the same thing to the groups_users table as well,
setting user_id = 0 for every record with group_id = (the group being
edited).
This all seems incredibly complex for what I thought was a very simple
goal. Have I gone about this completely wrong or is there just a
little more I need to change to get it working? Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment