Reputation: 411
hi all I'm trying to create a registration page where a person creates an account, then creates a user. The account can have multiple users and a user can have 0 or 1 accounts. The problem I'm getting with my current code is that account_id isnt loading from a select box
the user can have 0 or 1 account the account have 1 or many users
my table schema is
users -id, username, firstname, lastname
accounts - id, companyname
accounts_users -id, account_id, user_id
accounts model
<?php
class Account extends AppModel{
var $name='Account';
public $useTable = 'accounts';
public $primaryKey = 'id';
var $hasAndBelongsToMany = array(
'User' =>
array(
'className'=>'User',
)
);
users model
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 is the accounts controller
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.');
}
}
}
the users addAdmin function
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.'); }
}
}
the addAdmin form
<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');
?>
Upvotes: 0
Views: 135
Reputation: 6552
As I stated in comments, the error is not described clearly. Here is a first answer, I will update/correct it should the answer be ever updated.
The error may be in the controller:
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.'); }
}
The set function is not always called as hidden behind a if. Try:
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.'); }
}
Upvotes: 1