1

这另一个问题

在脚手架 Cakephp 中显示 ID 以外的其他内容

已在此处和其他地方的许多地方得到回答,并处理我想要一个选择列表但要显示的文本字段而不是 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'));
4

3 回答 3

1

在 CakePHP 3.x 中,最好的、有文档的和优雅的方法是:

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#customize-key-value-output

可以使用闭包来访问列表中的实体访问器方法,如下所示:

// In your Authors Entity create a virtual field to be used as the displayField:
protected function _getLabel()
{
    return $this->_properties['first_name'] . ' ' . $this->_properties['last_name']
      . ' / ' . __('User ID %s', $this->_properties['user_id']);
}

然后您可以直接使用以下方法在列表查找器中获取标签:

// In AuthorsTable::initialize():
$this->setDisplayField('label'); // Will utilize Author::_getLabel()
// In your finders/controller:
$query = $authors->find('list'); // Will utilize AuthorsTable::getDisplayField()
// In your views:
$author->label // will display : John Doe / User ID 123
于 2017-11-06T17:26:34.677 回答
0

我想你的意思是这样的:

// controller/action
$locations = $this->Visit->Location->find('list');
$referrers = $this->Visit->Referrer->find('list');
$companies = $this->Visit->Company->find('list');
$this->set(compact('locations', 'referrers', 'companies'));

在你看来:

echo $this->Form->input('location_id');
echo $this->Form->input('referrer_id');
echo $this->Form->input('company_id');

那(应该)产生三个选择 - 模仿display-field你所追求的行为。

引用蛋糕:

find('list', $params) 返回一个索引数组,可用于任何需要列表的用途,例如填充输入选择框。

http://book.cakephp.org/1.3/view/1022/find-list

于 2012-06-12T20:05:30.377 回答
0

在 CakePHP 3.x 中:

打开你的 App/Model/Table/Your_Table.php

public function initialize(array $config) {
    $this->displayField(['full_name', 'email']);
}

当您检索列表时:

TableRegistry::get('Your')->find('list');

结果将是:

[
    [key => 'full_name;email'],
    [key => 'full_name;email'],
];
于 2016-01-06T07:11:57.750 回答