2012-08-03 117 views
0

大家好我想创建一个注册页面,其中一个人创建一个帐户,然后创建一个用户。该帐户可以有多个用户,并且用户可以有0个或1个帐户。我与我当前的代码获得的问题是从选择框,ACCOUNT_ID心不是装载HABTM cakephp和保存

用户可以有0或1帐户 的帐户中有1个或多个用户

我的表架构是

用户 - id, username, firstname, lastname 账户 - id, companyname accounts_users - id, account_id, user_id

账户模式

<?php 

class Account extends AppModel{ 

    var $name='Account'; 
    public $useTable = 'accounts'; 
    public $primaryKey = 'id'; 
    var $hasAndBelongsToMany = array(
     'User' => 
      array(
       'className'=>'User', 
       ) 
      ); 

用户模型

class User extends AppModel{ 

     public $name = 'User'; 
     public $useTable = 'users'; 
     public $primaryKey = 'id'; 
     var $hasAndBelongsToMany = array(
      'Account' => 
       array(
        'className'=>'Account', 
        'joinTable'=>'accounts_users' 
        )); 

add函数是账户控制器

function add(){ 
     $this->set('title_for_layout', 'Account registration'); 
     $this->set('stylesheet_used', 'style'); 
     $this->set('image_used', 'eBOXLogo.jpg'); 

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

用户addAdmin功能

function addAdmin(){ 
    $this->set('title_for_layout', 'Please Login'); 
    $this->set('stylesheet_used', 'style'); 
    $this->set('image_used', 'eBOXLogo.jpg'); 
if($this->request->is('post')){ 
$this->User->create(); 
$this->set('accounts', $this->User->Account->find('list')); 
if ($this->User->save($this->request->data)) 
{ 
    $this->Session->setFlash('The user has been saved'); 
    $this->redirect(array('controller'=>'Users','action' => 'login')); 
} 
else { $this->Session->setFlash('The user could not be saved. Please, try again.'); } 
} 

}

的addAdmin形式

<h2>Welcome please add your Administrator Details</h2> 
<?php 
echo $this->Form->create('User', array('action'=>'add')); 
echo $this->Form->input('Account'); 
echo $this->Form->input('username',array('label'=>'Username: ')); 
echo $this->Form->input('password',array('label'=>'Password: ')); 
echo $this->Form->input('password_confirmation',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('street',array('label'=>'Street Address: ')); 
echo $this->Form->input('city',array('label'=>'City: ')); 
echo $this->Form->input('state',array('label'=>'State: ')); 
echo $this->Form->input('country',array('label'=>'Country: ')); 
echo $this->Form->input('access_level', array('default' => 2)); 
echo $this->Form->end('Add Administrator'); 

?> 
+0

“account_id未从选择框加载”您能否准确描述发生了什么。 – 2012-08-03 15:26:27

回答

1

正如我在评论中所述,错误没有被清楚地描述。这是第一个答案,如果答案不断更新,我会更新/更正答案。

该错误可能是在控制器:

function addAdmin(){ 
$this->set('title_for_layout', 'Please Login'); 
$this->set('stylesheet_used', 'style'); 
$this->set('image_used', 'eBOXLogo.jpg'); 

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

设定功能并不总是被称为隐藏在背后是否。尝试:

function addAdmin(){ 
$this->set('title_for_layout', 'Please Login'); 
$this->set('stylesheet_used', 'style'); 
$this->set('image_used', 'eBOXLogo.jpg'); 
$this->set('accounts', $this->User->Account->find('list'));//moved 

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' => 'login')); 
} 
else { $this->Session->setFlash('The user could not be saved. Please, try again.'); } 
} 
+0

真棒!解决了我的问题然而是否有可能加载该业务'abn? – user1393064 2012-08-03 22:12:40