Cakephp 保存记录后在条件上重定向

Cakephp 保存记录后在条件上重定向,cakephp,controller,Cakephp,Controller,我有一个用户控制器,如果我添加一个用户,我想根据用户在创建帐户时选择的用户类型重定向 情况: 用户表 身份证 名字 用户类型\用户id 用户添加表单有一个用户类型选择框,我有两种类型的用户:教师和学生(每个都是表、模型、控制器),如果用户选择我要重定向到的教师/teachers/add/$id如果用户选择我要重定向到的学生:/students/add/$id 这是我的自动取款机,但显然不起作用 <?php class UsersController extends AppControlle

我有一个用户控制器,如果我添加一个用户,我想根据用户在创建帐户时选择的用户类型重定向

情况:

用户表

身份证

名字

用户类型\用户id

用户添加表单有一个用户类型选择框,我有两种类型的用户:教师和学生(每个都是表、模型、控制器),如果用户选择我要重定向到的教师/teachers/add/$id如果用户选择我要重定向到的学生:/students/add/$id

这是我的自动取款机,但显然不起作用

<?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'));
}

}
?>

我很确定问题在于这样一种假设,即由于
$this->User->id
存在,
$this->User->usertype\u id
也必须存在,而它不存在。我第一次使用CakePHP时也遇到了这个问题

如果用户类型是通过添加表单传递的,则需要检查数据数组:

改变

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

如果这不起作用(我不记得成功保存后是否清空了
$this->data
),那么您应该在保存之前存储该值,如下所示:

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
附录
在我看来,这比在重定向中使用串联更简洁:

$this->redirect(array('controller' => 'teachers', 'action' => 'add', $id));
但我想这只是偏好

附录2
关于清理控制器和将所有逻辑移到模型中,我还有一些额外的建议。通过这种方式,您可以在将来重用来自其他控制器的代码,并且您当前的控制器将更易于阅读。我会将整个方法更改为如下所示:

// 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),
    );
}

谢谢,我想这是你必须知道的。我想我用谷歌搜索了121324个不同的东西来找到正确的方法,但没有任何结果。你让我开心:)
// 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),
    );
}