CakePHP-使用$this时视图中未定义的变量->;在控制器中设置(“variableName';”,$variable);

CakePHP-使用$this时视图中未定义的变量->;在控制器中设置(“variableName';”,$variable);,php,cakephp,Php,Cakephp,我试图在视图中为我的控制器设置一个变量,如下所示: 这是用户/add操作 public function add() { if ($this->request->is('post')) { $usertype = $this->User->Usertypes->find('list', array('fields' => array('id', 'description'))); $this->set('usertype

我试图在视图中为我的控制器设置一个变量,如下所示:

这是用户/add操作

public function add() { 
if ($this->request->is('post')) {
        $usertype = $this->User->Usertypes->find('list', array('fields' => array('id', 'description')));
        $this->set('usertype', $usertype);
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}
使用调试器::dump($usertype)时;我明白了

array(  (int) 1 => 'Staff',     (int) 2 => 'Administrator',     (int) 3 => 'Coordinator' )
所以$usertype的设置肯定是正确的。在我位于/view/Users/add.ctp的视图中,我尝试访问变量,如下所示:

这是查看/用户/添加.ctp

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('firstName');
        echo $this->Form->input('lastName');
        echo $this->Form->input('email');
        echo $this->Form->input('password');
        echo $this->Form->input('usertypes_id',array('options'=>$usertype));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

但是我得到一个错误“未定义变量:usertype[APP/View/Users/add.ctp,第10行]”


我对CakePHP比较陌生,我在这里做错了什么?

如果设置了变量,则必须在
之外设置变量(在之前或之后),否则将重定向到另一个页面,这意味着当请求是POST,但当您点击url时,请求类型默认为GET。

debug$usertype在add.ctpAlso中这一行之前,请提及控制器和操作的名称及其视图文件。在调用这两行之前,usertype为null。控制器名为UsersController,操作add()和视图add.ctp您需要显示整个控制器操作,错误的完整堆栈跟踪可能也很有用。有时在控制器操作中设置变量并不意味着它是在您试图使用它的环境中设置的。绝对传奇,我知道它会像这样简单,谢谢。