views:

31

answers:

1

hi,

I have a users controller, where if I add a user I want to redirect based on the usertype a user selects on making his account

the situation:

users table

id

name

usertype_id

The user add form has a select box for user type, I have two types of users: teachers and students (each another table, model, controller) if the user chooses teacher I want to redirect to /teachers/add/$id if the user chooses student I want to redirect to: /students/add/$id

this is what I have atm, but that doesn't work obviously

<?php
class UsersController extends AppController {
    var $name = 'Users';

    function add() {
    if (!empty($this->data)) {
        $this->User->create();
        if ($this->User->save($this->data)) {
            $id = $this->User->id;
            if ($this->User->usertype_id=='1')
            {
                $this->redirect(array('students/add/'.$id));
            } elseif ($this->User->usertype_id=='2') {
                $this->redirect(array('teachers/add/'.$id));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
        }
    }
    $usertypes = $this->User->Usertype->find('list');
    $this->set(compact('usertypes'));
}

}
?>
+1  A: 

I'm pretty sure the problem is the assumption that, because $this->User->id exists, $this->User->usertype_id must also exist, which it does not. I ran into that issue when I first started working with CakePHP, too. :)

If the user type was passed via the add form, you need to check the data array:

Change

if ($this->User->usertype_id=='1')

To

if ($this->data['User']['usertype_id'] == '1')

If that doesn't work (I can't remember if $this->data is emptied after a successful save) then you should store the value prior to the save, like so:

function add() {
    if (!empty($this->data)) {
        $usertype_id = $this->data['User']['usertype_id'];
        $this->User->create();
        if ($this->User->save($this->data)) {
            $id = $this->User->id;
            if ($usertype_id == '1') {
                $this->redirect(array('students/add/'.$id));
            } elseif ($usertype_id == '2') {
                // the rest remains the same

Addendum
Rather than using the concatenation in your redirect, This looks cleaner to me:

$this->redirect(array('controller' => 'teachers', 'action' => 'add', $id));

But I guess that's just preference.

Addendum 2
I have some additional advice about cleaning up your controller and moving all of the logic to the model. This way you can re-use the code from other controllers in the future, and your current controller will be easier to read. I would change the entire method to look like this:

// this is in /controllers/users_controller.php
function add() {
    if (!empty($this->data)) {
        $saved_user = $this->User->save_user($this->data);
        if ($saved_user['success']) {
            $this->redirect(array(
                'controller' => $saved_user['controller'],
                'action' => 'add',
                $this->User->id
            ));
        }
    }
    $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
    $usertypes = $this->User->Usertype->find('list');
    $this->set(compact('usertypes'));
}

// this is in your model, /models/user.php
function save_user($data) {
    $this->create;
    $usertype_id = $data['User']['usertype_id'];
    return array(
        'controller' => ($usertype_id == '2') ? 'teachers': 'students';
        'success' => $this->save($data),
    );
}
Stephen
thanks, just something you have to know I guess. I think I googled 121324 different things to find the right way to do this, without any results. You made my day :)
Weptunus
You're welcome!
Stephen

related questions