HABTM cakephp和保存

时间:2012-08-03 13:53:25

标签: cakephp associations

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

用户可以拥有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'
                    ));

添加功能是帐户控制器

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

?>

1 个答案:

答案 0 :(得分: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.'); } 
}