0

大家好我正在尝试创建一个注册页面,一个人创建一个帐户,然后创建一个用户。该帐户可以有多个用户,一个用户可以有 0 个或 1 个帐户。我当前的代码遇到的问题是 account_id 不是从选择框中加载的

用户可以有 0 个或 1 个帐户 该帐户有 1 个或多个用户

我的表架构是

用户 -id, username, firstname, lastname 帐户 -id, companyname 帐户用户 -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');

?>
4

1 回答 1

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.'); } 
} 

set 函数并不总是被称为隐藏在 if 后面。尝试:

 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.'); } 
}
于 2012-08-03T16:02:23.437 回答