cakephp会话数据和表格

时间:2012-08-20 04:00:41

标签: database forms cakephp

大家好我正在尝试创建一个表单,用户可以创建一个新用户,该用户与创建用户的人具有相同的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');
?>

1 个答案:

答案 0 :(得分: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']));