【问题标题】:CakePHP custom label for form input options表单输入选项的 CakePHP 自定义标签
【发布时间】:2012-08-08 09:52:17
【问题描述】:

我有一个创建发票的表单,发送方和接收方表单输入取自数据库中的可用列表。

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

每个用户只能属于一个帐户,但一个帐户可以有多个用户。 根据登录用户的身份,表单输入是 account_id。

在发票控制器中:

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

在 addInvoice 视图中:

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

在这种情况下,“公司名称”的标签是硬编码的,需要替换为动态标签,即 accounts 表中的 company_name

稍微复杂一点,接收者标签必须是 company_nameaccounts,或者如果 company_nameNULL,(所以他们是个人帐户,而不是企业帐户)它将是 @987654330 @来自users

尝试的解决方案: ***在InvoicesController 中创建$sendername 变量

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

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

那些似乎不起作用,想知道在 CakePHP 中是否可以实现?

【问题讨论】:

  • 我们更改了我们的数据库:1-M 用于帐户到用户,.. 所以不需要 AccountsUser。我们还将 company_name 更改为 account_name 以确保数据库有效性,.. 删除 NULL,也为了编码理智! :) 解决了!

标签: php forms cakephp label cakephp-2.0


【解决方案1】:
$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'),
    )
));

不确定这是否是您想要的,以及我是否正确理解了问题,但希望它能给您一些启发。

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2011-08-04
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多