CakePHP下拉列表用户名和公司名称

时间:2012-08-10 07:42:43

标签: mysql forms list cakephp dropdownbox

嘿,有一个页面允许企业向客户(个人和其他企业)发送发票。 Receiver输入字段允许用户选择将发票发送到哪个客户。目前,下拉列表列出了业务,但我们希望该列表也包括个人(用户模型/表)。因此,如果帐户(帐户模型/表)没有company_name,则显示用户的用户名。

以下是我试图尝试使其正常工作,不会出错,但在下拉列表中没有显示任何内容。我怎么能实现我们想要的呢?

发票控制器:

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

查看:

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

2 个答案:

答案 0 :(得分:0)

'Account.company_name' != null

显然总是正确的,因为字符串不为空。所以

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

始终$name = 'Account.company_name';

答案 1 :(得分:0)

您可以定义临时虚拟字段'display_name'和代码如下

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