0

大家好,我正在尝试创建一个表单,用户可以在其中创建一个新用户,该用户将具有与创建用户的人相同的 account_id。

目前我的页面在表单中显示了正确的 account_id,但是当我们将表单提交到数据库时,它保存为错误的东西。

这是我的功能

      function add_Employee(){

        $accounts=$this->User->find('list', array(
        'fields'=>array('account_id'),
        'conditions' => array(
        'User.id' => $this->Auth->user('id'))));

    if($this->request->is('post')){
    $this->User->create(); 
    if ($this->User->save($this->request->data)) 
    { 
        $this->Session->setFlash('The user has been saved');  
        $this->redirect( array('controller'=>'Users','action' => 'index_admin')); 
    }
    else { $this->Session->setFlash('The user could not be saved. Please, try again.'); } 
    }
    $this->set('accounts',$accounts);


  }

和视图

  <h2>Add Employee</h2>
<p>Add a new Employee here, please enter their details below.</p>
<?php
echo $this->Form->create('User', array('action'=>'add_Employee'));
echo $this->Form->input('username',array('label'=>'Username: '));
echo $this->Form->input('password',array('label'=>'Password: '), array('type'=>'password'));
echo $this->Form->input('password_confirmation',array('label'=>'Confirm: '), array('type'=>'password'));
echo $this->Form->input('email',array('label'=>'Email: '));
echo $this->Form->input('title',array('label'=>'Title: '));
echo $this->Form->input('firstname',array('label'=>'First Name: '));
echo $this->Form->input('surname',array('label'=>'Surname: '));
echo $this->Form->input('active', array('default'=>true, 'type'=>'hidden'));
echo $this->Form->input('account_id', array('value'=>$accounts['User']['account_id']));
echo $this->Form->input('access_level', array(
    'label' => 'Access Level:', 
    'options' => array('1'=>'Employee','2'=>'Adminstrator')));
echo $this->Form->end('Submit');
?>
4

1 回答 1

2

您必须将帐户 ID 作为值输入表单:

In controller:

 $accounts=$this->User->find('first', array(
    'conditions' => array(
    'id' => $this->Auth->user('id'))));
 $this->set('account',$accounts);

In view :
echo $this->Form->input('account_id', array('label'=>'Account','value'=>$account['User']['account_id']));
于 2012-08-20T04:37:34.400 回答