2
votes

a account hasAndBelongsToMany users

a user hasAndBelongsToMany account

a account hasMany templates

a template belongsTo an account

accounts can't log in, users can and act on behalf of the account. A user creates the template for the account, the problem with my database is that templates require the account_id so that other users employed by the account can access the template but the problem I'm having is that cake cant find the account_id related to the user and put it in the table for templates.

the related tables are

users - id, name, address accounts - id, companyname, abn accountss_users - id, user_id, account_id templates - id, name, description

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

     }

the template view

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

EDIT - the problem is the $accounts line, its currently loading the title into the dropdown box in the view, not the account. company namee and its throwing a database error.

when debugging $accounts it prints out this

array(
    (int) 0 => array(
        'User' => array(
            'password' => '*****',
            'id' => '14',
            'username' => 'cheese',
            'title' => 'mr',
            'first_name' => '',
            'surname' => 'hall',
            'email' => '[email protected]',
            '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

1 Answers

1
votes

Did you try with recursive?

In your Template controller:

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

In your Template view:

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

If you don't know what is inside your $current_user, put <?php debug($current_user); ?>.