2
votes

I have a form that creates an invoice, the Sender and Receiver form inputs are taken from an available list in the database.

accounts(id,...., company_name, abn) ---> id is primary
users(id, username, title, firstname, surname,...) ---> id is primary
accounts_users(id, account_id, user_id) --->account_id from accounts, user_id from users

Each user can only belong to one account, but one account can have many users. The form inputs are the account_id, according to who the logged in user is.

In the Invoices Controller:

 $accounts3=$this->User->AccountsUser->find('list', array(
        'fields'=>array('account_id','account_id'),'conditions' => array(
        'user_id' => $this->Auth->user('id'))));


$accounts=$this->User->Relationship->find('list', array('fields'=>array('receiver_id'),'conditions' =>  array('sender_id' => $accounts2)));

In the addInvoice View:

<?php
echo $this->Form->create('Invoice', array('action'=>'addinvoice'));
echo $this->Form->input('sender_id',array('label'=>'Sender: ', 'options' => array('$accounts3' => 'COMPANY NAME')));
echo $this->Form->input('receiver_id',array('label'=>'Receiver: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->input('active', array('label' => 'Schedule?', 'options' => array('1'=>'Send','0'=>'Schedule'), 'default'=>'1'));
echo $this->Form->hidden('templates_id', array('default'=>'0'));
echo $this->Form->end('Submit');

The label of 'COMPANY NAME' in this case is hard coded and needs to be replaced by a dynamic label which would be company_name from the accounts table.

A bit more complicated, the receiver label needs to be company_name from accounts, or if company_name is NULL, (so they're a personal, not a business account) it would be username from users.

Solutions attempted: ***creating $sendername variable in InvoicesController

$sendername=$this->User->Account->find('list', array('fields'=>array('company_name'),'conditions' => array('account_id' => $accounts3)));

OR

$sendername=$this->User->AccountsUser->find('list', array('fields'=>array('id','company_name'),'conditions' =>  array('user_id' => $this->Auth->user('id'))));

Those don't seem to work, wondering if it's even possible in CakePHP?

1
We changed our database: 1-M for accounts to users,.. So no need for AccountsUser. We also changed company_name to account_name for database validity,.. Remove NULLs, and also for coding sanity! :) so resolved! - Michelle Dobson

1 Answers

0
votes
$accounts3 = $this->User->AccountsUser->Account->find('list', array(
    'fields' => array('company_name'),
    'joins' => array(
        array(
            'alias' => 'AccountsUser',
            'table' => 'accounts_users',
            'type' => 'left',
            'conditions' => array('AccountsUser.account_id = Account.id'),
        )    
    ),
    'conditions' => array(
        'AccountsUser.user_id' => $this->Auth->user('id'),
    )
));

// find all accounts with id => label from accounts table
// where AccountsUser.user_id = id of logged in user
// label being user.username when company_name is null or company_null if not null
$this->User->AccountsUser->Account->virtualFields['label'] = 'CASE WHEN Account.company_name IS NULL THEN User.username ELSE Account.company_name END';
$accounts = $this->User->AccountsUser->Account->find('list', array(
    'fields' => array('label'),
    'joins' => array(
        array(
            'alias' => 'AccountsUser',
            'table' => 'accounts_users',
            'type' => 'left',
            'conditions' => array('AccountsUser.account_id = Account.id'),
        ),
        array(
            'alias' => 'User',
            'table' => 'users',
            'type' => 'left',
            'conditions' => array('AccountsUser.user_id = User.id'),
        ),    
    ),
    'conditions' => array(
        'AccountsUser.user_id' => $this->Auth->user('id'),
    )
));

Not sure if this is what you wanted and if I understood the problem correctly, but hopefully it will give you something to go off of.