Question

Hey have got a page which allows businesses to send invoices to customers (both individuals and other businesses). The Receiver input field allows the user to select which customer to send the invoice to. At the moment the drop down lists businesses, but we want that list to include individuals (User model/table) too. So if the account (Account model/table) doesn't have a company_name then display the user's username.

Below is what I have tried to try and get it working, doesn't give errors, but doesn't show anything on the drop down list. How could I achieve what we want?

Invoice Controller:

$name = "";
if ('Account.company_name' != null) {
    $name = 'Account.company_name';
} else {
    $name = 'User.username';
}

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

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

$receivername = $this->Account->find('list', array(
    'fields' => array($name),
    'conditions' => array(
        'id' => $accounts)));

View:

echo $this->Form->input('receiver_id', array(
    'label' => 'Receiver: ',
    'type' => 'select',
    'options' => $receivername));
Was it helpful?

Solution

'Account.company_name' != null

is obviously always true, as a string is not null. So

if ('Account.company_name' != null) {
    $name = 'Account.company_name';
} else {
    $name = 'User.username';
}

always do $name = 'Account.company_name';

OTHER TIPS

you can define a temporary virtual field 'display_name' and code like below

$this->Account->virtualFields['display_name'] = "IF(Account.company_name='' OR Account.company_name IS NULL, (SELECT u.name FROM users u WHERE u.account_id = Account.id),Account.company_name)";

$list = $this->Account->find('list',array(
   'fields' => array('Account.id','Account.display_name')
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top