Saturday, June 2, 2012

saveAssociated vs multiple saves and which controller or model?

I have the need to create an action called "changeOwner"  that will
change a bunch of fields in several associated tables, then return to
the calling page.  It does not have its own view, so it does not get
data to fill out a form and post it back, like the edit action would.

I would like to use the saveAssociated call instead of multiple saves,
because saveAssociated can be atomic, otherwise I would have to
implement this logic somewhere.

My question is - does it make sense for the controller action to build
out all data for the save? or should this be somewhere else?
This seems like a bad idea because I am putting some model specific data
in the controller. Is this ok? or is there a better way to do this?

example

in organizationsController.php

public function changeOwner($orgId, $newOwner) {

     $mr = $this->Organization->MemberRelationship->find('first',
array('conditions' => array('MemberRelationship.id' => $newOwner)));
     $ownerRole = $this->Organization->OrganizationRole->find('first',
array('conditions' => array('OrganizationRole.name' => 'owner',
                                                                         
                                    'OrganizationRole.organization_id'
=> $orgId )));
     $dataTbl['organization']['id'] = $orgId;
     $dataTbl['organization']['owner_user_id'] = $mr['user_id'];
     $dataTbl['MemberRelationship']['id'] = $newOwner;
     $dataTbl['MemberRelationship']['organization_role_id'] =
$ownerRole['id'];

     if ($this->Organization->saveAssociated($dataTbl)) {
                $this->Session->setFlash(__('Owner Updated'));
     } else {
                 $this->Session->setFlash(__('Error trying to change
owner. Please Try again.'));
     }
     $this->redirect($this->referer()));
}




View this message in context: saveAssociated vs multiple saves and which controller or model?
Sent from the CakePHP mailing list archive at Nabble.com.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
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

No comments: