这另一个问题
已在此处和其他地方的许多地方得到回答,并处理我想要一个选择列表但要显示的文本字段而不是 id 未命名为“名称”的情况。显然,如果您通过在其模型中添加以下内容来告诉它该字段被命名为“名称”而不是“名称”,Cake 可以处理此问题:
var $displayField = 'NonNameName';
然而,所有的例子都是一个选择。但是我有三个选择列表,那么如何添加它们?当然,我不能按照以下代码所示进行操作(例如,三行 "var $displayField = "...";" .. 拥有三个 $displayField .. 没有意义。
<?php
App::uses('AppModel', 'Model');
/**
* Visit Model
*
* @property User $User
* @property Referrer $Referrer
* @property Company $Company
*/
class Visit extends AppModel {
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
var $displayField = 'location';
var $displayField = 'referrer';
var $displayField = 'company';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Referrer' => array(
'className' => 'Referrer',
'foreignKey' => 'referrer_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Location' => array(
'className' => 'Location',
'foreignKey' => 'location_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Company' => array(
'className' => 'Company',
'foreignKey' => 'company_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
在我的控制器中,我有
$companies= $this->Visit->Company->find('list');
$locations = $this->Visit->Location->find('list', array('conditions' => array('company_id' => $this->Auth->user('company_id'))));
$referrers = $this->Visit->Referrer->find('list', array('conditions' => array('company_id' => $this->Auth->user('company_id'))));
$this->set(compact('locations','referrers','companies'));