Saturday, April 4, 2009

Re: Updating users in group edit view

I've read here that hasMany doesn't work with automagic forms
automatically. So how to map, for example, checkboxes in the view file
to each group_id of users of the data array in my case?

Thanks, Ivan

On Apr 2, 5:19 pm, Givano <giv...@gmail.com> wrote:
> Hi All!
>
> I hope you can help me, I've tried everything I could but it still not
> working and should be very simple thing.
>
> I have Users table:
> CREATE TABLE `users` (
>   `id` int(11) unsigned NOT NULL auto_increment,
>   `username` varchar(50) collate utf8_unicode_ci NOT NULL,
>   `realname` varchar(150) collate utf8_unicode_ci default NULL,
>   `emailaddress` varchar(120) collate utf8_unicode_ci default NULL,
>   `location_id` int(11) unsigned NOT NULL,
>   `group_id` int(11) unsigned default NULL,
>   PRIMARY KEY  (`id`)
> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
>
> and Groups table:
> CREATE TABLE `groups` (
>   `id` int(11) unsigned NOT NULL auto_increment,
>   `name` varchar(120) collate utf8_unicode_ci NOT NULL,
>   PRIMARY KEY  (`id`)
> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
>
> both Models are defined as
>
> class Group extends AppModel {
>         var $name = 'Group';
>         var $hasMany = array('User');
>
> }
>
> class User extends AppModel {
>         var $name = 'User';
>         var $belongsTo = array('Group');
>
> }
>
> and I have generated standard methods for controllers.
>
> My goal is to make edit view for group where I have multiple selection
> of users with criteria (all users which have the same group_id as the
> group I'm editing or have group_id is null) and on submit all selected
> users should get id of this group assigned.
>
> Controller function edit:
>         function edit($id = null) {
>                 if (!$id && empty($this->data)) {
>                         $this->Session->setFlash(__('Invalid Group', true));
>                         $this->redirectBack();
>                 }
>                 if (!empty($this->data)) {
>                         if ($this->Group->save($this->data)) {
>                                 $this->Session->setFlash(__('The Group has been saved', true));
>                                 $this->redirectBack();
>                         } else {
>                                 $this->Session->setFlash(__('The Group could not be saved. Please,
> try again.', true));
>                         }
>                 }
>                 if (empty($this->data)) {
>                         $this->data = $this->Group->read(null, $id);
>                 }
>                 $users= $this->Group->User->find('list','fields'=>'User.realname');
>                 $this->set(compact('users'));
>         }
> My view edit.ctp:
> <div class="groups form">
> <?php echo $form->create('Group');?>
>         <fieldset>
>                 <legend><?php __('Edit Group');?></legend>
>         <?php
>                 pr($users);
>                 echo $form->input('id');
>                 echo $form->input('name');
>                 echo $form->input('location_id');
>                 echo $form->input('User',array
> ('type'=>'select','multiple'=>'checkbox'));
>
>         ?>
>         </fieldset>
> <?php echo $form->end('Submit');?>
>
> The Problem is that on submit only group object is saved, no changes
> on user data. Please, please help me on it!
>
> TIA,
>
> Ivan

--~--~---------~--~----~------------~-------~--~----~
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: