Just to make sure I understand what you said about putting code into the
models....
For the most part, anything that verifies, modifies, reads data from the
DB should be in the model.
The controller, just collects data, directs the models to
read/change/update the data, then send to the view for presentation.
so this would be a good controller function ...
function viewMembers($orgId) {
$this->Organization->id = $orgId;
$this->set('user', $this->Auth->user());
if (!$this->Organization->exists()) {
throw new NotFoundException(__('Invalid organization'));
}
$this->set('organization', $this->Organization->read(null,
$orgId));
if (!isset($this->MemberRelationship))
$this->loadModel('MemberRelationship');
$this->MemberRelationship->recursive = 0;
$this->set('memberRelationships',
$this->paginate('MemberRelationship',
array('MemberRelationship.organization_id' => $orgId)));
$this->set('perms', $this->perms);
}
And this would be a good example of a controller functions that needs to
have the part setting default values in request->data moved to a model
function. Right?
public function add() {
$user = $this->Auth->user();
if ($this->request->is('post')) {
// ****** move following to a Model Function ***********
$this->request->data['Organization']['active'] = true; //
default value
$this->request->data['Organization']['valid_until'] =
date('Y-m-d h:i:s', strtotime('+1 week')); // default value
$this->request->data['OrgShipAddress']['first_name'] =
$user['first_name']; // set default value from user
$this->request->data['OrgShipAddress']['middle_name'] =
$user['middle_name']; // set default value from user
$this->request->data['OrgShipAddress']['last_name'] =
$user['last_name']; // set default value from user
$this->request->data['OrgShipAddress']['company'] =
$this->data['Organization']['name']; // set default value from user
$this->request->data['Organization']['contact_user_id'] =
$user['id']; // set default value from user
$this->request->data['Organization']['alt_contact_user_id']
= $user['id']; // set default value from user
$this->request->data['OrgShipAddress']['user_id'] =
$user['id']; // set default value from user
if ( $this->request->data['Organization']['BillingSame']) {
// use same address for billing
unset($this->request->data['OrgBillAddress']);
$this->request->data['OrgShipAddress']['user_id'] =
$user['id']; // set default value from user
} else {
$this->request->data['OrgBillAddress']['first_name'] =
$user['first_name']; // set default value from user
$this->request->data['OrgBillAddress']['middle_name'] =
$user['middle_name']; // set default value from user
$this->request->data['OrgBillAddress']['last_name'] =
$user['last_name']; // set default value from user
$this->request->data['OrgBillAddress']['company'] =
$this->data['Organization']['name']; // set default value from user
$this->request->data['OrgBillAddress']['user_id'] =
$user['id']; // set default value from user
}
// ********* above should be done by a Model Function *******
$this->Organization->create();
if ($ret =
$this->Organization->saveAssociated($this->request->data)) {
if ( $this->request->data['Organization']['BillingSame']) {
$result =
$this->Organization->saveField('bill_address_id',$this->Organization->OrgShipAddress->id);
} else {
$result3 =
$this->Organization->OrgBillAddress->saveField('organization_id',$this->Organization->id);
}
$result2 =
$this->Organization->OrgShipAddress->saveField('organization_id',$this->Organization->id);
$orgId = $this->Organization->getLastInsertId();
if (!isset($this->OrganizationRole))
$this->loadModel('OrganizationRole');
$ret = $this->OrganizationRole->createDefaultRoles($orgId);
if ($ret == false) {
$this->Session->setFlash(__('Default Org Roles not
created.'));
} else {
$role = $this->OrganizationRole->find('first', array(
'fields' =>
array('OrganizationRole.id','OrganizationRole.name','OrganizationRole.active'),
'conditions' =>
array('OrganizationRole.organization_id' => $orgId,
'OrganizationRole.name' => 'owner'),
'recursive' => 0));
$ret = false;
if ( isset($role['OrganizationRole']['id'])) {
if (!isset($this->MemberRelationship))
$this->loadModel('MemberRelationship');
$ret =
$this->MemberRelationship->addRelationship($user, $orgId,
$role['OrganizationRole']['id']);
}
if ($ret) {
$this->Session->setFlash(__('The organization
has been saved'));
} else {
$this->Session->setFlash(__('The User /
organization relationship could not be saved. Please, try again.'));
}
}
$this->redirect(array('action' => 'view',
$this->Organization->id));
} else {
$this->Session->setFlash(__('The organization could not
be saved. Please, try again.'));
}
}
}
View this message in context: Re: Understanding MVC Architecture
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
Tuesday, April 24, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment