I'm new to Cake, and to MVC's in general. I want to establish good habits from the get go. Among the good habits are keeping controllers lean, and making the models fat. But it's a bit of a moving target for a noob like me. If I need to pass info from one model to another, do I just dump all that into the controller? Try to make it work in a model?
Here's an action that is a prime example of the kind of confusion I'm trying to sort out.
Everything seems like it should be in the controller, but I'm likely wrong. This action gets a list of members, sends that to a view. In the view I can tick members whose accounts I want "activate." No ACL, just simple auth. I make sure that "sub-admins" only see the users they're allowed to manage by use of a db field, client_id. The two models I'm using are User and Client.
public function activate() {
if ($this->request->is('get')) {
$id = $this->Auth->user('id');
$this->User->id = $id; //make sure current User is the logged in user
$currentClient = $this->User->field('client_id'); // get client_id based on logged in user
$members = $this->User->Client->find('first', array( // find users that have the same client_id
'conditions' => array('id' => $currentClient),
'recursive' => 1
));
$this->set('clients', $members); // send the users to the view
} else if ($this->request->is('post') || $this->request->is('put')) {
$members = $this->request->data['Members']; // grab players submitted from push form
$memberIds = array(); // this will hold the selected users
foreach($members as $a){
$memberIds[$a['id']] = $a['id']; // loop over user's that were selected
}
$usersToActivate = $this->User->find('all', array( //find user records, based on the array of id's
'conditions' => array(
"User.id" => $memberIds
)
));
$this->Ticket->bulkActivate($usersToActivate); // send array of members into model for processing
$this->Session->setFlash('Activations sent.', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
}
}
To my eye, it doesn't look drastically wrong... and I'm already doing some processing in the model (as seen with the bulkActivate that actually takes the user records, and generates activation tickets).
But I can't help but feel that it's not quite 100% yet.