Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:04:40+00:00 2026-05-17T22:04:40+00:00

I have a users controller, where if I add a user I want to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:04:40+00:00Added an answer on May 17, 2026 at 10:04 pm

    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),
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a users controller. For a specific user I want to have something
I have an entries controller that allows users to add contact information the website.
I have controller users . In this controller I have action account which is
I have a posts controller on a small test website I'm making. I want
I have an edit action in the users controller. What I want to do
I have a User table with id , name , e-mail etc... i want
I'd like to have an Index action for a Users controller that takes an
I have a form which users must fill out and submit. The controller action
I currently have this as a search: <%= form_tag users_path, :controller => 'users', :action
I have a controller user with a method login For some reason, when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.