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_name
从accounts
,或者如果company_name
是NULL
,(所以他们个人,而不是一个企业帐户),这将是从users
username
。
解决方案尝试: ***在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的甚至可能吗?
我们改变了我们的数据库:1-M帐户给用户,因此不需要AccountsUser。我们还将company_name更改为account_name以获得数据库有效性。删除NULL,并且还为编码理智! :)解决了! – 2012-08-22 17:04:57