【问题标题】:cakephp session data from two tables来自两个表的 cakephp 会话数据
【发布时间】:2012-08-05 09:50:25
【问题描述】:

一个帐户有AndBelongsToMany用户

用户拥有AndBelongsToMany 帐户

一个帐户有许多模板

一个模板属于一个帐户

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

相关表是

用户 - id、姓名、地址 帐户 - id、公司名称、abn accounts_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'
                )
            )
        )
    )
)

【问题讨论】:

    标签: session cakephp


    【解决方案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 中的内容,请输入 &lt;?php debug($current_user); ?&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 2012-05-12
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多