大家好我正在尝试创建一个注册页面,一个人创建一个帐户,然后创建一个用户。该帐户可以有多个用户,一个用户可以有 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');
?>