2012-08-03 120 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