来自两个表的cakephp会话数据

时间:2012-08-05 09:50:26

标签: session cakephp

帐户hasAndBelongsToMany用户

用户hasAndBelongsToMany帐户

帐户有多个模板

模板属于帐户

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

相关表格是

用户 - 身份证,姓名,地址 帐户 - id,companyname,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'
                )
            )
        )
    )
)

1 个答案:

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

相关问题