문제

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));
도움이 되었습니까?

해결책

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

다른 팁

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')
));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top