2

一个帐户 hasAndBelongsToMany users

用户 hasAndBelongsToMany 帐户

一个账户有很多模板

一个模板属于一个帐户

帐户无法登录,用户可以代表该帐户行事。用户为该帐户创建模板,我的数据库的问题是模板需要 account_id,以便该帐户使用的其他用户可以访问该模板,但我遇到的问题是蛋糕找不到与该用户相关的 account_id并将其放在模板表中。

相关表格是

用户 - id、姓名、地址 帐户 - id、公司名称、abn accountss_users - id、user_id、account_id 模板 - id、名称、描述

 function add() {
        $this->set('title_for_layout', 'Please Enter Your Temaplate Details');
        $this->set('stylesheet_used', 'style');
        $this->set('image_used', 'eBOXLogo.jpg');  

        if($this->request->is('post')){ $accounts=$this->User->find('all', array('f' => array('Account.companyName'), 'conditions' => array('User.id' => $this->Auth->user('id')), 'contain' => array('accountsuser.user_id')));
    $this->Template->create();

          if ($this->Template->save($this->request->data)) {
            $this->Session->setFlash('The template has been saved');
            $this->redirect( array('controller' => 'Fields','action' => 'add'));
          } else {
            $this->Session->setFlash('The template could not be saved. Please, try again.');
          }
        }

        $this->set('accounts', $accounts); 

     }

模板视图

<h2>Please enter the details of your Template</h2></br>
<p>After Hitting Submit You Can Start Adding Various Fields</p>

<?php
echo $this->Form->create('Template', array('action'=>'add'));
echo $this->Form->input('name',array('label'=>'Template Name: '));
echo $this->Form->input('account_id',array('label'=>'Business: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->input('description',array('label'=>'Short Description Of Template: '));
echo $this->Form->end('Click Here To Submit Template');
?>

编辑 - 问题是 $accounts 行,它当前将标题加载到视图的下拉框中,而不是帐户。公司名称及其引发数据库错误。

调试 $accounts 时,它会打印出这个

array(
    (int) 0 => array(
        'User' => array(
            'password' => '*****',
            'id' => '14',
            'username' => 'cheese',
            'title' => 'mr',
            'first_name' => '',
            'surname' => 'hall',
            'email' => 'jtg987@hotmail.com',
            'date_joined' => null,
            'active' => true,
            'access_level' => '1'
        ),
        'Relationship' => array(),
        'Account' => array(
            (int) 0 => array(
                'id' => '10',
                'street' => '6 ridley court',
                'city' => 'doncaster',
                'postcode' => '3109',
                'state' => 'vic',
                'country' => 'australia',
                'active' => false,
                'company_name' => 'kfc',
                'abn' => '99999',
                'AccountsUser' => array(
                    'id' => '3',
                    'account_id' => '10',
                    'user_id' => '14'
                )
            )
        )
    )
)
4

1 回答 1

1

你试过递归吗?

在您的模板控制器中:

$this->User->recursive = 2; //now you will get businesses related to the users
$current_user = $this->User->findById($id_of_the_user); //find the current user
$this->set('current_user', $current_user);

在您的模板视图中:

<?php
echo $this->Form->create('Template', array('action'=>'add'));
echo $this->Form->input('name',array('label'=>'Template Name: '));
echo $this->Form->input('description',array('label'=>'Short Description Of Template: '));
echo $this->Form->input('business_id', array('type' => 'hidden', 'value' => $current_user['Business']['id']));
echo $this->Form->end('Click Here To Submit Template');
?>

如果您不知道 $current_user 中的内容,请将<?php debug($current_user); ?>.

于 2012-08-05T10:23:43.157 回答