2012-08-07 90 views
2

我有一個創建發票的表單,發送方和接收方表單輸入取自數據庫中的可用列表。用於表單輸入選項的CakePHP自定義標籤

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,(所以他們個人,而不是一個企業帳戶),這將是從usersusername

解決方案嘗試: ***在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')))); 

那些創造$sendername變量似乎不工作,不知道是否在CakePHP的甚至可能嗎?

+0

我們改變了我們的數據庫:1-M帳戶給用戶,因此不需要AccountsUser。我們還將company_name更改爲account_name以獲得數據庫有效性。刪除NULL,並且還爲編碼理智! :)解決了! – 2012-08-22 17:04:57

回答

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

不知道這是否是你想要的東西,如果我理解正確的問題,但希望它會給你的東西走下車的。

相關問題