我正在尝试在视图中为我的控制器设置一个变量,如下所示:
这是用户/添加操作
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.'));
}
}
}
当使用 Debugger::dump($usertype); 我明白了
array( (int) 1 => 'Staff', (int) 2 => 'Administrator', (int) 3 => 'Coordinator' )
所以 $usertype 肯定设置正确。在我在 /View/Users/add.ctp 找到的视图中,我尝试像这样访问变量:
这是视图/用户/add.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>
但是我收到一个错误“未定义的变量:用户类型 [APP/View/Users/add.ctp,第 10 行]”
我对 CakePHP 比较陌生,我在这里做错了什么?